193 lines
7.0 KiB
C#
193 lines
7.0 KiB
C#
using System;
|
|
|
|
namespace SteamWare
|
|
{
|
|
/// <summary>
|
|
/// classe gestione auth
|
|
/// </summary>
|
|
public class authProxy
|
|
{
|
|
#region area table adapters
|
|
|
|
/// <summary>
|
|
/// The ta anag dev
|
|
/// </summary>
|
|
public DS_devicesTableAdapters.AnagDevicesTableAdapter taAnagDev;
|
|
|
|
/// <summary>
|
|
/// init dei table adapters
|
|
/// </summary>
|
|
protected void initTA()
|
|
{
|
|
taAnagDev = new DS_devicesTableAdapters.AnagDevicesTableAdapter();
|
|
}
|
|
/// <summary>
|
|
/// effettua setup dei connection strings da web.config della singola applicazione
|
|
/// </summary>
|
|
protected virtual void setupConnectionStringBase()
|
|
{
|
|
// connections del db
|
|
string connString = memLayer.ML.confReadString("DevicesConnectionString");
|
|
taAnagDev.Connection.ConnectionString = connString;
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="authProxy"/> class.
|
|
/// </summary>
|
|
protected authProxy()
|
|
{
|
|
initTA();
|
|
setupConnectionStringBase();
|
|
}
|
|
/// <summary>
|
|
/// Singleton accesso a authProxy
|
|
/// </summary>
|
|
public static authProxy AP = new authProxy();
|
|
|
|
/// <summary>
|
|
/// Tenta autologin con autoriconoscimento Dominio/username by cookie
|
|
/// </summary>
|
|
/// <param name="cookieName"></param>
|
|
/// <returns></returns>
|
|
public static bool tryAuthByCookie(string cookieName)
|
|
{
|
|
logger.lg.scriviLog(String.Format(string.Format("Richiesta login da cookie {0}", cookieName)), tipoLog.INFO);
|
|
string devSecret = memLayer.ML.getCookieVal(cookieName);
|
|
bool answ = false;
|
|
DS_devices.AnagDevicesRow device = null;
|
|
if (devSecret != "" && devSecret != null)
|
|
{
|
|
// cerco il device...ogni dipendente può averne + di 1 registrato a suo nome...
|
|
string Dominio = "";
|
|
string UsrName = "";
|
|
try
|
|
{
|
|
device = DataWrap.DW.taAnagDev.getByDeviceSecret(devSecret)[0];
|
|
Dominio = device.Dominio;
|
|
UsrName = device.User_Name;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
logger.lg.scriviLog(String.Format("Errore recupero dati da devSecret {0}:{1}{2}", devSecret, Environment.NewLine, exc), tipoLog.ERROR);
|
|
}
|
|
if (Dominio != "" && UsrName != "")
|
|
{
|
|
answ = true;
|
|
try
|
|
{
|
|
// avvio l'utente (in sessione)
|
|
user_std.UtSn.startUpUtente(Dominio, UsrName);
|
|
logger.lg.scriviLog(String.Format("Effettuata login da cookie per l'utente {0}/{1}", Dominio, UsrName), tipoLog.INFO);
|
|
}
|
|
catch
|
|
{ }
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
/// <summary>
|
|
/// formatta il secret code
|
|
/// </summary>
|
|
/// <param name="Dominio"></param>
|
|
/// <param name="UsrName"></param>
|
|
/// <param name="matrOpr"></param>
|
|
/// <param name="DeviceName"></param>
|
|
/// <param name="adesso"></param>
|
|
/// <returns></returns>
|
|
public static string getSecret(string Dominio, string UsrName, int matrOpr, string DeviceName, DateTime adesso)
|
|
{
|
|
return string.Format("{0}|{1}|{2}|{3}|{4}", Dominio, UsrName, DeviceName, matrOpr, adesso);
|
|
}
|
|
/// <summary>
|
|
/// crea un nuovo record device e salva un nuovo cookie su db x il dispositivo e l'utente richiesti
|
|
/// </summary>
|
|
/// <param name="Dominio"></param>
|
|
/// <param name="UsrName"></param>
|
|
/// <param name="matrOpr"></param>
|
|
/// <param name="DeviceName"></param>
|
|
/// <param name="Description"></param>
|
|
/// <param name="IPv4"></param>
|
|
/// <param name="cookieName"></param>
|
|
/// <param name="expDate"></param>
|
|
/// <returns></returns>
|
|
public static bool createNewCookie(string Dominio, string UsrName, int matrOpr, string DeviceName, string Description, string IPv4, string cookieName, DateTime expDate)
|
|
{
|
|
bool answ = false;
|
|
// calcolo il secret...
|
|
DateTime adesso = DateTime.Now;
|
|
string Secret = authProxy.getSecret(Dominio, UsrName, matrOpr, DeviceName, adesso);
|
|
string devSecret = SteamCrypto.EncryptString(Secret, cookieName);
|
|
try
|
|
{
|
|
// genero nuovo record!
|
|
DataWrap.DW.taAnagDev.insertQuery(devSecret, Dominio, UsrName, matrOpr, DeviceName, Description, adesso, IPv4);
|
|
// salvo il cookie nel browser
|
|
memLayer.ML.setCookieVal(cookieName, devSecret, expDate);
|
|
logger.lg.scriviLog(String.Format("Salvato login da cookie per l'utente {0}/{1} - matr {2}", Dominio, UsrName, matrOpr), tipoLog.INFO);
|
|
// indico come fatto
|
|
answ = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
logger.lg.scriviLog(string.Format("Errore in salvataggio cookie su db/dispositivo:{0}dominio:{1} | userName:{2} | matr:{6} | deviceName:{3} | ip:{4}{0}{5}", Environment.NewLine, Dominio, UsrName, DeviceName, IPv4, exc, matrOpr));
|
|
}
|
|
return answ;
|
|
}
|
|
/// <summary>
|
|
/// rimuove device da DB e toglie il cookie
|
|
/// </summary>
|
|
/// <param name="DeviceSecret">secret associata al device</param>
|
|
/// <returns></returns>
|
|
public static bool removeDeviceByDevSec(string DeviceSecret)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
DataWrap.DW.taAnagDev.delByDeviceSecret(DeviceSecret);
|
|
answ = true;
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
/// <summary>
|
|
/// rimuove device da DB e toglie il cookie
|
|
/// </summary>
|
|
/// <param name="DeviceName">Nome Device</param>
|
|
/// <returns></returns>
|
|
public static bool removeDeviceByDevName(string DeviceName)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
DataWrap.DW.taAnagDev.delByDeviceName(DeviceName);
|
|
answ = true;
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
/// <summary>
|
|
/// imuove device da DB e toglie il cookie
|
|
/// </summary>
|
|
/// <param name="Dominio"></param>
|
|
/// <param name="UsrName"></param>
|
|
/// <returns></returns>
|
|
public static bool removeDeviceByUserDominio(string Dominio, string UsrName)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
DataWrap.DW.taAnagDev.delByDominioUser(Dominio, UsrName);
|
|
answ = true;
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
}
|
|
}
|