using System; namespace SteamWare { /// /// classe gestione auth /// public class authProxy { #region area table adapters /// /// The ta anag dev /// public DS_devicesTableAdapters.AnagDevicesTableAdapter taAnagDev; /// /// init dei table adapters /// protected void initTA() { taAnagDev = new DS_devicesTableAdapters.AnagDevicesTableAdapter(); } /// /// effettua setup dei connection strings da web.config della singola applicazione /// protected virtual void setupConnectionStringBase() { // connections del db string connString = memLayer.ML.confReadString("DevicesConnectionString"); taAnagDev.Connection.ConnectionString = connString; } #endregion /// /// Initializes a new instance of the class. /// protected authProxy() { initTA(); setupConnectionStringBase(); } /// /// Singleton accesso a authProxy /// public static authProxy AP = new authProxy(); /// /// Tenta autologin con autoriconoscimento Dominio/username by cookie /// /// /// 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; } /// /// formatta il secret code /// /// /// /// /// /// /// 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); } /// /// crea un nuovo record device e salva un nuovo cookie su db x il dispositivo e l'utente richiesti /// /// /// /// /// /// /// /// /// /// 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; } /// /// rimuove device da DB e toglie il cookie /// /// secret associata al device /// public static bool removeDeviceByDevSec(string DeviceSecret) { bool answ = false; try { DataWrap.DW.taAnagDev.delByDeviceSecret(DeviceSecret); answ = true; } catch { } return answ; } /// /// rimuove device da DB e toglie il cookie /// /// Nome Device /// public static bool removeDeviceByDevName(string DeviceName) { bool answ = false; try { DataWrap.DW.taAnagDev.delByDeviceName(DeviceName); answ = true; } catch { } return answ; } /// /// imuove device da DB e toglie il cookie /// /// /// /// public static bool removeDeviceByUserDominio(string Dominio, string UsrName) { bool answ = false; try { DataWrap.DW.taAnagDev.delByDominioUser(Dominio, UsrName); answ = true; } catch { } return answ; } } }