Files
MoonPro.net/MP-Admin/WebUserControls/mod_login.ascx.cs
T
2018-10-19 10:39:33 +02:00

310 lines
12 KiB
C#

using SteamWare;
using System;
using System.Web.UI;
/// <summary>
/// classe gestione login e forzatura login
/// </summary>
public partial class mod_login : SteamWare.ApplicationUserControl
{
#region area protected/private
#region area proprietà
private SteamWare.loginMode _isForceUser = SteamWare.loginMode.normale;
#endregion
#region area metodi
/// <summary>
/// imposta la modalità di login tra normale / forceUser
/// </summary>
private void setLoginMode()
{
switch (_isForceUser)
{
case SteamWare.loginMode.normale:
pnlForceUser.Visible = false;
pnlSelectUser.Visible = false;
break;
case SteamWare.loginMode.forceUser:
pnlForceUser.Visible = true;
pnlSelectUser.Visible = false;
break;
case SteamWare.loginMode.standardUser:
pnlForceUser.Visible = false;
pnlSelectUser.Visible = true;
break;
default:
break;
}
}
protected override void traduciObj()
{
lblPwd.Text = user_std.UtSn.Traduci("lblPwd");
lblUser.Text = user_std.UtSn.Traduci("lblUser");
lblDominio.Text = user_std.UtSn.Traduci("lblDominio");
lblTitolo.Text = user_std.UtSn.Traduci("ForzaUtente");
btnOk.Text = user_std.UtSn.Traduci("btnCommit");
btnOkUserStd.Text = user_std.UtSn.Traduci("btnCommit");
}
/// <summary>
/// prova a verificare se l'utente sia ok x AD credentials
/// </summary>
private void AdLogin()
{
lblMessage.Text = "User not authenticated...";
if (Page.User.Identity.IsAuthenticated)
{
//recupera user windows se c'è...
string ad_name = Page.User.Identity.Name;
string delimStr = "\\";
char[] delimiter = delimStr.ToCharArray();
string[] dom_user = ad_name.Split(delimiter, 2);
// passo al controllo di verifica ADuserOk...
user_std _utente = new user_std();
if (_utente.ADuserOk(dom_user[0], dom_user[1]))
{
bool fatto = _utente.startUpUtente(dom_user[0], dom_user[1]);
if (fatto)
{
SteamWare.logger.lg.scriviLog(string.Format("L'utente {0} ({1}) ha effettuato il login correttamente", _utente.CognomeNome, _utente.userNameAD), SteamWare.tipoLog.INFO);
if (Login_ok != null)
{
Login_ok(this, new EventArgs());
}
}
else
{
lblMessage.Text = String.Format("{0}<br>There are some problems instatiating user: {1}/{2}", user_std.UtSn.Traduci("AccessFail"), dom_user[0], dom_user[1]);
SteamWare.logger.lg.scriviLog(String.Format("Accesso fallito, problemi ad istanziare l'utente {0}/{1}", dom_user[0], dom_user[1]), SteamWare.tipoLog.ERROR);
if (Login_Error != null)
{
Login_Error(this, new EventArgs());
}
}
}
else
{
lblMessage.Text = String.Format("{0}<br>user not allowed: {1}/{2}", user_std.UtSn.Traduci("AccessFail"), dom_user[0], dom_user[1]);
SteamWare.logger.lg.scriviLog(String.Format("Utente non autorizzato: {0}/{1}", dom_user[0], dom_user[1]), SteamWare.tipoLog.WARNING);
if (Login_Error != null)
{
Login_Error(this, new EventArgs());
}
}
}
else
{
lblMessage.Text = user_std.UtSn.Traduci("AccessFail") + user_std.UtSn.Traduci("UsrNotAuth");
SteamWare.logger.lg.scriviLog(String.Format("Accesso fallito, utente non autenticato"), SteamWare.tipoLog.WARNING);
if (Login_Error != null)
{
Login_Error(this, new EventArgs());
}
}
}
/// <summary>
/// effettua verifiche e se concesso permette di forzare l'accesso utente
/// </summary>
private void ForceUserIdentity()
{
if (Page.User.Identity.IsAuthenticated)
{
bool _allowForceUser = false;
try
{
_allowForceUser = SteamWare.memLayer.ML.CRB("_allowForceUser");
}
catch
{
_allowForceUser = false;
}
if (_allowForceUser)
{
if (authKey.Text == "forzaInter") // verifica passphrase...
{
user_std _utente = new user_std();
user_std.UtSn.isForcedUser = true;
bool fatto = _utente.startUpUtente(dominio.Text, user.Text);
if (fatto)
{
string _rigaLog = String.Format("User {0} has forced user identity ok: logged as \t {1}\\{2}", Page.User.Identity.Name, dominio.Text, user.Text);
SteamWare.logger.lg.scriviLog(_rigaLog, SteamWare.tipoLog.INFO);
if (Login_ok != null)
{
Login_ok(this, new EventArgs());
}
}
}
else
{
lblMessage.Text = String.Format("{0}<br>key not allowed for operation!! operation logged!!", user_std.UtSn.Traduci("AccessFail"));
mandaEmail(_fromEmail, _adminEmail, "Attenzione: tentativo di accesso non autorizzato!", String.Format("Tentativo di forcing user non autorizzato!<br>L'utente {0} ha tentato di accedere a {1} forzando l'utente ma la sua key autorizzativa e' sbagliata...", Page.User.Identity.Name, user_std.UtSn.Traduci(SteamWare.memLayer.ML.CRS("defaultApp"))));
string _rigaLog = String.Format("User {0}\t tried to force user - wrong password - he tried to log as \t {1}\\{2}", Page.User.Identity.Name, dominio.Text, user.Text);
SteamWare.logger.lg.scriviLog(_rigaLog, SteamWare.tipoLog.WARNING);
if (Login_Error != null)
{
Login_Error(this, new EventArgs());
}
}
}
else
{
mandaEmail(_fromEmail, _adminEmail, "Attenzione: tentativo di accesso non autorizzato!", String.Format("Tentativo di forcing user non autorizzato!<br>L'utente {0} ha tentato di accedere a {1} forzando l'utente ma la funzione e' disabilitata...", Page.User.Identity.Name, user_std.UtSn.Traduci(SteamWare.memLayer.ML.CRS("defaultApp"))));
string _rigaLog = String.Format("User {0}\t tried to force user - access disabled - he tried to log as \t {1}\\{2}", Page.User.Identity.Name, dominio.Text, user.Text);
SteamWare.logger.lg.scriviLog(_rigaLog, SteamWare.tipoLog.WARNING);
if (Login_Error != null)
{
Login_Error(this, new EventArgs());
}
}
}
else
{
lblMessage.Text = string.Format("{0}<br>user not authenticated!<br>", user_std.UtSn.Traduci("AccessFail"));
if (Login_Error != null)
{
Login_Error(this, new EventArgs());
}
string _rigaLog = String.Format("\t Someone tried to force user - real user: \t - not autenticated - \t tried to log as \t {0}\\{1}", dominio.Text, user.Text);
SteamWare.logger.lg.scriviLog(_rigaLog, SteamWare.tipoLog.WARNING);
}
}
/// <summary>
/// se concesso il generico ForceUser permette di forzare l'accesso utente ad uno degli standard
/// </summary>
private void StdUserIdentity()
{
if (Page.User.Identity.IsAuthenticated)
{
bool _allowForceUser = false;
try
{
_allowForceUser = SteamWare.memLayer.ML.CRB("_allowForceUser");
}
catch
{
_allowForceUser = false;
}
if (_allowForceUser)
{
//leggo e codifico utente indicato
string delimStr = "\\";
char[] delimiter = delimStr.ToCharArray();
string[] dom_user = ddlStdUser.SelectedValue.Split(delimiter, 2);
//forzo login!
user_std _utente = new user_std();
user_std.UtSn.isForcedUser = true;
bool fatto = _utente.startUpUtente(dom_user[0], dom_user[1]);
if (fatto)
{
string _rigaLog = String.Format("User {0} has forced user identity ok: logged as \t {1}\\{2}", Page.User.Identity.Name, dom_user[0], dom_user[1]);
SteamWare.logger.lg.scriviLog(_rigaLog, SteamWare.tipoLog.INFO);
if (Login_ok != null)
{
Login_ok(this, new EventArgs());
}
}
}
else
{
mandaEmail(_fromEmail, _adminEmail, "Attenzione: tentativo di accesso non autorizzato!", String.Format("Tentativo di forcing user non autorizzato!<br>L'utente {0} ha tentato di accedere a {1} forzando l'utente ma la funzione e' disabilitata...", Page.User.Identity.Name, user_std.UtSn.Traduci(SteamWare.memLayer.ML.CRS("defaultApp"))));
string _rigaLog = String.Format("User {0}\t tried to force user - access disabled - he tried to log as \t {1}", Page.User.Identity.Name, ddlStdUser.SelectedValue);
SteamWare.logger.lg.scriviLog(_rigaLog, SteamWare.tipoLog.WARNING);
if (Login_Error != null)
{
Login_Error(this, new EventArgs());
}
}
}
else
{
lblMessage.Text = string.Format("{0}<br>user not authenticated!<br>", user_std.UtSn.Traduci("AccessFail"));
if (Login_Error != null)
{
Login_Error(this, new EventArgs());
}
string _rigaLog = String.Format("\t Someone tried to force user - real user: \t - not autenticated - \t tried to log as \t {0}\\{1}", dominio.Text, user.Text);
SteamWare.logger.lg.scriviLog(_rigaLog, SteamWare.tipoLog.WARNING);
}
}
/// <summary>
/// fa login con force user e controllo pwd
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnOk_Click(object sender, EventArgs e)
{
ForceUserIdentity();
}
/// <summary>
/// fa login utente tipo standard
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnOkUserStd_Click(object sender, EventArgs e)
{
StdUserIdentity();
}
#endregion
#endregion
#region area public
#region eventi pubblici esposti
public event EventHandler Login_ok;
public event EventHandler Login_Error;
#endregion
#region area proprietà
/// <summary>
/// modalità funzionamento controllo tra normale (ActiveDirectory e user auth di default) e forceUser
/// </summary>
public SteamWare.loginMode modoLogin
{
get
{
return _isForceUser;
}
set
{
_isForceUser = value;
}
}
#endregion
/// <summary>
/// avvio pagina
/// </summary>
protected override void Page_Load(object sender, EventArgs e)
{
base.Page_Load(sender, e);
//carico da web.config i default values
loadDefaultsWebConfig();
// procedo...
setLoginMode();
Session.RemoveAll();
if (_isForceUser == SteamWare.loginMode.normale)
{
AdLogin();
}
}
#endregion
}