diff --git a/GPW.CORE.Comp/SteamCrypto.cs b/GPW.CORE.Comp/SteamCrypto.cs new file mode 100644 index 0000000..2d0e6a4 --- /dev/null +++ b/GPW.CORE.Comp/SteamCrypto.cs @@ -0,0 +1,214 @@ +using System.Security.Cryptography; +using System.Text; + +namespace GPW.CORE.Comp +{ + /// + /// utils x cifrature e Crypto + /// + public class SteamCrypto + { + #region Public Methods + + /// + /// decifra un messaggio con una password + /// + /// + /// + /// + public static string DecryptString(string Message, string Passphrase) + { + string answ = Message; + byte[] Results = new byte[8]; + UTF8Encoding UTF8 = new UTF8Encoding(); + + // Step 1. We hash the passphrase using MD5 We use the MD5 hash generator as the result + // is a 128 bit byte array which is a valid length for the TripleDES encoder we use below + + var HashProvider = MD5.Create(); + //MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider(); + byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase)); + + // Step 2. Create a new TripleDESCryptoServiceProvider object + var TDESAlgorithm = TripleDES.Create(); + //TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider(); + + // Step 3. Setup the decoder + TDESAlgorithm.Key = TDESKey; + TDESAlgorithm.Mode = CipherMode.ECB; + TDESAlgorithm.Padding = PaddingMode.PKCS7; + + // Step 4. Convert the input string to a byte[] + byte[] DataToDecrypt = new byte[8]; + try + { + DataToDecrypt = Convert.FromBase64String(Message); + } + catch + { } + if (DataToDecrypt != null) + { + // Step 5. Attempt to decrypt the string + try + { + ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor(); + Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length); + } + finally + { + // Clear the TripleDes and Hashprovider services of any sensitive information + TDESAlgorithm.Clear(); + HashProvider.Clear(); + } + // Step 6. Return the decrypted string in UTF8 format + answ = UTF8.GetString(Results); + } + return answ; + } + + /// + /// cifra un messaggio con una password + /// + /// + /// + /// + public static string EncryptString(string Message, string Passphrase) + { + byte[] Results; + UTF8Encoding UTF8 = new UTF8Encoding(); + + // Step 1. We hash the passphrase using MD5 We use the MD5 hash generator as the result + // is a 128 bit byte array which is a valid length for the TripleDES encoder we use below + + var HashProvider = MD5.Create(); + //MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider(); + byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase)); + + // Step 2. Create a new TripleDESCryptoServiceProvider object + var TDESAlgorithm = TripleDES.Create(); + //TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider(); + + // Step 3. Setup the encoder + TDESAlgorithm.Key = TDESKey; + TDESAlgorithm.Mode = CipherMode.ECB; + TDESAlgorithm.Padding = PaddingMode.PKCS7; + + // Step 4. Convert the input string to a byte[] + byte[] DataToEncrypt = UTF8.GetBytes(Message); + + // Step 5. Attempt to encrypt the string + try + { + ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor(); + Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length); + } + finally + { + // Clear the TripleDes and Hashprovider services of any sensitive information + TDESAlgorithm.Clear(); + HashProvider.Clear(); + } + + // Step 6. Return the encrypted string as a base64 encoded string + return Convert.ToBase64String(Results); + } + + /// + /// genera hash di una stringa in MD5 (es x hash gravatar) + /// + /// + /// + public static string getHashStringMD5(string Message) + { + string hash = ""; + using (MD5 md5Hash = MD5.Create()) + { + hash = GetMd5Hash(md5Hash, Message); + } + return hash; + } + + /// + /// Crea un hash MD5 + /// + /// + /// + /// + public static string GetMd5Hash(MD5 md5Hash, string input) + { + // Convert the input string to a byte array and compute the hash. + byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input)); + + // Create a new Stringbuilder to collect the bytes and create a string. + StringBuilder sBuilder = new StringBuilder(); + + // Loop through each byte of the hashed data and format each one as a hexadecimal string. + for (int i = 0; i < data.Length; i++) + { + sBuilder.Append(data[i].ToString("x2")); + } + + // Return the hexadecimal string. + return sBuilder.ToString(); + } + + /// + /// Generates a random string with a given size + /// + /// + /// + /// + public static string RandomString(int size, bool lowerCase = false) + { + var builder = new StringBuilder(size); + + // Unicode/ASCII Letters are divided into two blocks (Letters 65–90 / 97–122): The first + // group containing the uppercase letters and the second group containing the lowercase. + + // char is a single Unicode character + char offset = lowerCase ? 'a' : 'A'; + const int lettersOffset = 26; // A...Z or a..z: length=26 + + for (var i = 0; i < size; i++) + { + var @char = (char)_random.Next(offset, offset + lettersOffset); + builder.Append(@char); + } + + return lowerCase ? builder.ToString().ToLower() : builder.ToString(); + } + + /// + /// Verify a hash against a string. + /// + /// + /// + /// + /// + public static bool VerifyMd5Hash(MD5 md5Hash, string input, string hash) + { + // Hash the input. + string hashOfInput = GetMd5Hash(md5Hash, input); + + // Create a StringComparer an compare the hashes. + StringComparer comparer = StringComparer.OrdinalIgnoreCase; + + if (0 == comparer.Compare(hashOfInput, hash)) + { + return true; + } + else + { + return false; + } + } + + #endregion Public Methods + + #region Private Fields + + private static readonly Random _random = new Random(); + + #endregion Private Fields + } +} \ No newline at end of file diff --git a/GPW.CORE.Data/DbModels/AnagDeviceModel.cs b/GPW.CORE.Data/DbModels/AnagDeviceModel.cs index 460766d..4c8d538 100644 --- a/GPW.CORE.Data/DbModels/AnagDeviceModel.cs +++ b/GPW.CORE.Data/DbModels/AnagDeviceModel.cs @@ -8,7 +8,7 @@ namespace GPW.CORE.Data.DbModels // // This is here so CodeMaid doesn't reorganize this document // - [Table("AnagDevice")] + [Table("AnagDevices")] public partial class AnagDeviceModel { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] diff --git a/GPW.CORE.SMART/Components/CmpTop.razor.cs b/GPW.CORE.SMART/Components/CmpTop.razor.cs index 1cd53b2..a028740 100644 --- a/GPW.CORE.SMART/Components/CmpTop.razor.cs +++ b/GPW.CORE.SMART/Components/CmpTop.razor.cs @@ -26,12 +26,14 @@ namespace GPW.CORE.Smart.Components protected IJSRuntime JSRuntime { get; set; } = null!; [Inject] protected MessageService MService { get; set; } = null!; + [Inject] + protected CoreSmartDataService CDService { get; set; } = null!; - protected AnagDeviceModel registeredDevice { get; set; } = new AnagDeviceModel(); - protected DipendentiModel registeredUser { get; set; } = new DipendentiModel(); + //protected AnagDeviceModel registeredDevice { get; set; } = new AnagDeviceModel(); + //protected DipendentiModel registeredUser { get; set; } = new DipendentiModel(); - protected string currIp { get; set; } = ""; - protected string deviceSecret { get; set; } = ""; + //protected string currIp { get; set; } = ""; + //protected string deviceSecret { get; set; } = ""; protected string Nome { get; set; } = ""; protected string Cognome { get; set; } = ""; protected int idxDipendente { get; set; } = 0; @@ -41,26 +43,105 @@ namespace GPW.CORE.Smart.Components } - protected override async Task OnAfterRenderAsync(bool firstrender = false) + protected override async Task OnAfterRenderAsync(bool firstrender) { await Task.Delay(1); - if (MService.RigaDip != null) - { - Nome = MService.RigaDip.Nome; - Cognome = MService.RigaDip.Cognome; - idxDipendente = MService.RigaDip.IdxDipendente; - } - if (MService.RigaDip != null) - { - navManager.NavigateTo("/", true); - } - else + if (firstrender) { if (!navManager.Uri.Contains("jumper")) { - navManager.NavigateTo("jumper", true); + await checkUser(); } } } + + /// + /// Verifica dati utente e pagina + /// + private async Task checkUser() + { + // se idxDip NON valido + if (MService.IdxDipendente <= 0) + { + await tryDeviceLogin(); + } + else + { + if (idxDipendente <= 0) + { + setupUserData(); + } + } + } + + private void setupUserData() + { + if (MService.RigaDip != null && MService.IdxDipendente > 0) + { + idxDipendente = MService.IdxDipendente; + Cognome = MService.RigaDip.Cognome; + Nome = MService.RigaDip.Nome; + StateHasChanged(); + } + } + + + /// + /// Prova login da dati Device (in LocalStorage) + /// + /// + private async Task tryDeviceLogin() + { + // cerco in localstorage il devicesecret + string devSecret = await MService.getDevSecretAsync(); + await Task.Delay(50); + if (!string.IsNullOrEmpty(devSecret)) + { + // cerco sul DB... + var rigaDev = await CDService.DeviceBySecret(devSecret); + // se trovato + if (rigaDev != null) + { + // recupero dati dip e inizializzo message service + var elencoDip = await CDService.DipendentiGetAll(); + if (elencoDip != null) + { + var rigaDip = elencoDip.FirstOrDefault(x => x.IdxDipendente == rigaDev.IdxDipendente); + MService.RigaDip = rigaDip; + // salvo in sessStorage idxDip... + await MService.setIdxDipAsync(rigaDev.IdxDipendente); + setupUserData(); + //navManager.NavigateTo("/", false); + } + } + else + { + navManager.NavigateTo("RegNewDevice"); + } + + } + else + { + navManager.NavigateTo("RegNewDevice"); + } + //if (MService.RigaDip != null) + //{ + // Nome = MService.RigaDip.Nome; + // Cognome = MService.RigaDip.Cognome; + // idxDipendente = MService.RigaDip.IdxDipendente; + //} + //if (MService.RigaDip != null) + //{ + // navManager.NavigateTo("/", true); + //} + //else + //{ + // if (!navManager.Uri.Contains("jumper")) + // { + // navManager.NavigateTo("jumper", true); + // } + //} + } + } } \ No newline at end of file diff --git a/GPW.CORE.SMART/Data/CoreSmartDataService.cs b/GPW.CORE.SMART/Data/CoreSmartDataService.cs index 82ad027..8dbf4f7 100644 --- a/GPW.CORE.SMART/Data/CoreSmartDataService.cs +++ b/GPW.CORE.SMART/Data/CoreSmartDataService.cs @@ -1,4 +1,5 @@ -using GPW.CORE.Data.DbModels; +using GPW.CORE.Comp; +using GPW.CORE.Data.DbModels; using GPW.CORE.Data.DTO; using Microsoft.AspNetCore.Identity.UI.Services; using Microsoft.Extensions.Caching.Distributed; @@ -110,8 +111,6 @@ namespace GPW.CORE.Smart.Data Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); List? dbResult = new List(); - - string? rawData = await redisDb.StringGetAsync(rKeyDipendenti); if (!string.IsNullOrEmpty(rawData)) { @@ -136,11 +135,42 @@ namespace GPW.CORE.Smart.Data { dbResult = new List(); } - stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; Log.Debug($"DipendentiGetAll | {source} in: {ts.TotalMilliseconds} ms"); - return await Task.FromResult(dbResult); + return dbResult; + } + + public async Task DeviceBySecret(string devSecret) + { + string source = "DB"; + Stopwatch stopWatch = new Stopwatch(); + stopWatch.Start(); + AnagDeviceModel? dbResult = new AnagDeviceModel(); + dbResult = dbController.AnagDeviceByKey(devSecret); + stopWatch.Stop(); + TimeSpan ts = stopWatch.Elapsed; + Log.Debug($"DeviceBySecret | {source} in: {ts.TotalMilliseconds} ms"); + await Task.Delay(1); + return dbResult; + } + + /// + /// Crea un record device + /// + /// + /// + public async Task DeviceInsert(AnagDeviceModel newRecord) + { + string source = "DB"; + Stopwatch stopWatch = new Stopwatch(); + stopWatch.Start(); + bool fatto = dbController.AnagDeviceInsert(newRecord); + stopWatch.Stop(); + TimeSpan ts = stopWatch.Elapsed; + Log.Debug($"DeviceInsert | {source} in: {ts.TotalMilliseconds} ms"); + await Task.Delay(1); + return fatto; } public void Dispose() @@ -161,6 +191,17 @@ namespace GPW.CORE.Smart.Data { dbController.rollBackEntity(item); } + public string EncriptData(string rawData) + { + return SteamCrypto.EncryptString(rawData, passPhrase); + } + + public string DeriptData(string encData) + { + return SteamCrypto.DecryptString(encData, passPhrase); + } + + protected const string passPhrase = "EB6BD8BE-638F-481E-85E4-F89012DA95BB"; #endregion Public Methods @@ -265,8 +306,6 @@ namespace GPW.CORE.Smart.Data private readonly IEmailSender _emailSender; - private readonly IMemoryCache memoryCache; - private List cachedDataList = new List(); /// @@ -332,8 +371,6 @@ namespace GPW.CORE.Smart.Data { await redisDb.KeyDeleteAsync(item); } - // brutalmente rimuovo intero contenuto DB... DANGER - //await server.FlushDatabaseAsync(); answ = true; } } @@ -377,7 +414,7 @@ namespace GPW.CORE.Smart.Data { dbResult = new List(); } - return await Task.FromResult(dbResult); + return dbResult; } /// @@ -412,7 +449,7 @@ namespace GPW.CORE.Smart.Data { dbResult = new List(); } - return await Task.FromResult(dbResult); + return dbResult; } /// @@ -447,7 +484,7 @@ namespace GPW.CORE.Smart.Data { dbResult = new List(); } - return await Task.FromResult(dbResult); + return dbResult; } /// @@ -479,7 +516,7 @@ namespace GPW.CORE.Smart.Data TimeSpan ts = stopWatch.Elapsed; Log.Trace($"Effettuata lettura da DB + caching per AnagFasiSearch: {ts.TotalMilliseconds} ms"); } - return await Task.FromResult(dbResult); + return dbResult; } /// @@ -512,7 +549,7 @@ namespace GPW.CORE.Smart.Data TimeSpan ts = stopWatch.Elapsed; Log.Trace($"Effettuata lettura da DB + caching per AnagFasiByProj: {ts.TotalMilliseconds} ms"); } - return await Task.FromResult(dbResult); + return dbResult; } /// /// Elenco Giustificativi @@ -543,7 +580,7 @@ namespace GPW.CORE.Smart.Data TimeSpan ts = stopWatch.Elapsed; Log.Trace($"Effettuata lettura da DB + caching per AnagGiust: {ts.TotalMilliseconds} ms"); } - return await Task.FromResult(dbResult); + return dbResult; } /// /// Recupera l'elenco gruppi abilitati x il dipendente @@ -578,7 +615,7 @@ namespace GPW.CORE.Smart.Data { dbResult = new List(); } - return await Task.FromResult(dbResult); + return dbResult; } /// @@ -614,7 +651,7 @@ namespace GPW.CORE.Smart.Data { dbResult = new AnagOrariModel(); } - return await Task.FromResult(dbResult); + return dbResult; } /// @@ -649,7 +686,7 @@ namespace GPW.CORE.Smart.Data { dbResult = new List(); } - return await Task.FromResult(dbResult); + return dbResult; } /// @@ -669,7 +706,7 @@ namespace GPW.CORE.Smart.Data } } - return await Task.FromResult(dbResult); + return dbResult; } /// @@ -706,7 +743,7 @@ namespace GPW.CORE.Smart.Data { dbResult = new CalcOreFasiModel(); } - return await Task.FromResult(dbResult); + return dbResult; } /// @@ -743,7 +780,7 @@ namespace GPW.CORE.Smart.Data { dbResult = new CalcOreProgettiModel(); } - return await Task.FromResult(dbResult); + return dbResult; } /// @@ -780,7 +817,7 @@ namespace GPW.CORE.Smart.Data { dbResult = new List(); } - return await Task.FromResult(dbResult); + return dbResult; } /// @@ -819,7 +856,7 @@ namespace GPW.CORE.Smart.Data { dbResult = new List(); } - return await Task.FromResult(dbResult); + return dbResult; } /// @@ -856,7 +893,7 @@ namespace GPW.CORE.Smart.Data { dbResult = new List(); } - return await Task.FromResult(dbResult); + return dbResult; } /// @@ -960,7 +997,7 @@ namespace GPW.CORE.Smart.Data { dbResult = new List(); } - return await Task.FromResult(dbResult); + return dbResult; } /// @@ -1001,7 +1038,7 @@ namespace GPW.CORE.Smart.Data { dbResult = new List(); } - return await Task.FromResult(dbResult); + return dbResult; } public async Task RegAttDelete(RegAttivitaModel currItem) @@ -1028,7 +1065,7 @@ namespace GPW.CORE.Smart.Data public async Task RegAttLastByDip(int IdxDipendente, bool onlyActive) { RegAttivitaModel dbResult = dbController.RegAttLastByDip(IdxDipendente, onlyActive); - return await Task.FromResult(dbResult); + return dbResult; } public async Task RegAttUpdate(RegAttivitaModel currItem) @@ -1121,7 +1158,7 @@ namespace GPW.CORE.Smart.Data { dbResult = new List(); } - return await Task.FromResult(dbResult); + return dbResult; } /// /// Restituisce elenco malattie globali @@ -1156,7 +1193,7 @@ namespace GPW.CORE.Smart.Data { dbResult = new List(); } - return await Task.FromResult(dbResult); + return dbResult; } /// @@ -1280,7 +1317,7 @@ namespace GPW.CORE.Smart.Data { dbResult = new List(); } - return await Task.FromResult(dbResult); + return dbResult; } /// @@ -1364,7 +1401,7 @@ namespace GPW.CORE.Smart.Data { dbResult = new List(); } - return await Task.FromResult(dbResult); + return dbResult; } public async Task RilTempUpdate(RilievoTempModel currItem) @@ -1400,7 +1437,7 @@ namespace GPW.CORE.Smart.Data stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; Log.Trace($"Effettuata lettura da DB + caching per TimbratureDay: {ts.TotalMilliseconds} ms"); - return await Task.FromResult(dbResult); + return dbResult; } public async Task TimbratureDelete(TimbratureModel currItem) diff --git a/GPW.CORE.SMART/Data/MessageService.cs b/GPW.CORE.SMART/Data/MessageService.cs index e3da46c..3ed30ba 100644 --- a/GPW.CORE.SMART/Data/MessageService.cs +++ b/GPW.CORE.SMART/Data/MessageService.cs @@ -1,30 +1,30 @@ -using GPW.CORE.Data; +using Blazored.LocalStorage; +using Blazored.SessionStorage; +using GPW.CORE.Data; using GPW.CORE.Data.DbModels; -using System.Security.Cryptography; -using System.Text; +using Microsoft.AspNetCore.Session; +using Org.BouncyCastle.Asn1; namespace GPW.CORE.Smart.Data { public class MessageService { - #region Private Fields - - private string _pageIcon = ""; - - private string _pageName = ""; - - private DipendentiModel? _rigaDip; - private string _searchVal = ""; - private bool showSearch; - - #endregion Private Fields - #region Public Fields public int orarioDip = 0; #endregion Public Fields + #region Public Constructors + + public MessageService(ILocalStorageService genLocalStorage, ISessionStorageService sessStore) + { + localStorage = genLocalStorage; + sessionStore = sessStore; + } + + #endregion Public Constructors + #region Public Events public event Action EA_HideSearch = null!; @@ -108,7 +108,6 @@ namespace GPW.CORE.Smart.Data } public bool PayloadOk { get; set; } = false; - public RegAttivitaModel? recordRA { get; set; } = null; public DipendentiModel? RigaDip @@ -118,8 +117,8 @@ namespace GPW.CORE.Smart.Data { if (_rigaDip != value) { + // salvo _rigaDip = value; - if (EA_SearchUpdated != null) { EA_SearchUpdated?.Invoke(); @@ -146,7 +145,6 @@ namespace GPW.CORE.Smart.Data } public int selWeekNum { get; set; } = 0; - public int selYear { get; set; } = DateTime.Today.Year; public bool ShowSearch @@ -179,6 +177,88 @@ namespace GPW.CORE.Smart.Data #endregion Public Properties + #region Public Methods + + + + + /// + /// Restituisce il valore di DeviceSecret da localstorage + /// + /// + public async Task getDevSecretAsync() + { + string answ = ""; + var result = await localStorage.GetItemAsync("DevSec"); + if (result != null) + { + answ = result; + } + return answ; + } + + /// + /// Scrive il valore di DeviceSecret nel localstoragee + /// + /// + /// + public async Task setDevSecretAsync(string devSec) + { + bool answ = false; + await localStorage.SetItemAsync("DevSec", devSec); + answ = true; + return answ; + } + + /// + /// Restituisce il valore di idxDip da localstorage + /// + /// + public async Task getIdxDipAsync() + { + int answ = -2; + var result = await sessionStore.GetItemAsync("idxDip"); + if (result != null) + { + answ = result; + } + return answ; + } + + /// + /// Scrive il valore di idxDip nel localstoragee + /// + /// + /// + public async Task setIdxDipAsync(int idxDip) + { + bool answ = false; + await sessionStore.SetItemAsync("idxDip", idxDip); + answ = true; + return answ; + } + + #endregion Public Methods + + #region Protected Properties + + protected ILocalStorageService localStorage { get; set; } = null!; + protected ISessionStorageService sessionStore { get; set; } = null!; + + #endregion Protected Properties + + #region Private Fields + + private string _pageIcon = ""; + + private string _pageName = ""; + + private DipendentiModel? _rigaDip; + private string _searchVal = ""; + private bool showSearch; + + #endregion Private Fields + #region Private Methods private void ReportPageUpd() diff --git a/GPW.CORE.SMART/GPW.CORE.SMART.csproj b/GPW.CORE.SMART/GPW.CORE.SMART.csproj index 9d0a60f..3ba7b78 100644 --- a/GPW.CORE.SMART/GPW.CORE.SMART.csproj +++ b/GPW.CORE.SMART/GPW.CORE.SMART.csproj @@ -20,6 +20,7 @@ + diff --git a/GPW.CORE.SMART/Pages/CheckDevice.razor b/GPW.CORE.SMART/Pages/CheckDevice.razor new file mode 100644 index 0000000..bb5e9fb --- /dev/null +++ b/GPW.CORE.SMART/Pages/CheckDevice.razor @@ -0,0 +1,7 @@ +@page "/CheckDevice" + +

CheckDevice

+ +@code { + +} diff --git a/GPW.CORE.SMART/Pages/Jumper.razor.cs b/GPW.CORE.SMART/Pages/Jumper.razor.cs index 4cfcfbc..2960549 100644 --- a/GPW.CORE.SMART/Pages/Jumper.razor.cs +++ b/GPW.CORE.SMART/Pages/Jumper.razor.cs @@ -18,6 +18,8 @@ using Blazored.LocalStorage; using Microsoft.AspNetCore.WebUtilities; using GPW.CORE.Smart.Data; using GPW.CORE.Data.DbModels; +using Org.BouncyCastle.Asn1.Ocsp; +using System.Net; namespace GPW.CORE.Smart.Pages { @@ -40,10 +42,27 @@ namespace GPW.CORE.Smart.Pages protected List? currDipList; protected DipendentiModel? currDip; - protected override async Task OnInitializedAsync() + protected override async Task OnAfterRenderAsync(bool firstRender) + { + await Task.Delay(50); + if (firstRender) + { + await tryUrlLogin(); + } + } + + private Random rnd = new Random(); + + + /// + /// Prova login da dati URL + /// + /// + private async Task tryUrlLogin() { - //MService.RigaDip = null; var uri = navManager.ToAbsoluteUri(navManager.Uri); + await Task.Delay(1); + // recupero da URL if (QueryHelpers.ParseQuery(uri.Query).TryGetValue("idxDipendente", out var _idxDipendente)) { if (!string.IsNullOrEmpty(_idxDipendente)) @@ -58,17 +77,58 @@ namespace GPW.CORE.Smart.Pages authKey = _authKey; } } - + string devName = ""; + DateTime adesso = DateTime.Now; + string dtData = $"{DateTime.UtcNow:yyyy-MM-dd_ZHH:mm:ss}"; + string devAgent = await JSRuntime.InvokeAsync("getUserAgent"); + var dimension = await JSRuntime.InvokeAsync("getWindowDimensions"); + string screenSize = $"{dimension.Width}x{dimension.Height}"; + string devIp = $""; + string randData = SteamCrypto.RandomString(32, true); currDipList = await CDService.DipendentiGetAll(); currDip = currDipList.Where(x => x.IdxDipendente == idxDipendente && x.AuthKey == authKey).FirstOrDefault(); - - MService.RigaDip = currDip; - if (currDip != null) { - navManager.NavigateTo(navManager.Uri, true); - } + // verifico authKey... + if (currDip.AuthKey == authKey) + { + // creo la DeviceSecret + string devSec = CDService.EncriptData($"{dtData}|{idxDipendente}|{devName}|{randData}"); + // ...e la salvo sul DB + AnagDeviceModel newDev = new AnagDeviceModel() + { + IdxDipendente = idxDipendente, + DeviceSecret = devSec, + DataOraEnabled = adesso, + DataOraLastSeen = adesso, + ScreenSize = screenSize, + DeviceName = devName, + Description = devAgent, + LastIpv4 = devIp + }; + bool fatto = await CDService.DeviceInsert(newDev); + if (fatto) + { + // ...e nel browser tramite message service + await MService.setDevSecretAsync(devSec); + await MService.setIdxDipAsync(idxDipendente); + // infine salvo dati dipendente e rimando a login + MService.RigaDip = currDip; + navManager.NavigateTo("/", true); + } + } + else + { + navManager.NavigateTo("RegNewDevice"); + } + } + } + + public class WindowDimension + { + public int Width { get; set; } + public int Height { get; set; } } } } \ No newline at end of file diff --git a/GPW.CORE.SMART/Pages/RegNewDevice.razor b/GPW.CORE.SMART/Pages/RegNewDevice.razor new file mode 100644 index 0000000..0ae2471 --- /dev/null +++ b/GPW.CORE.SMART/Pages/RegNewDevice.razor @@ -0,0 +1,20 @@ +@page "/RegNewDevice" + +

RegNewDevice

+ +Pagina x richiesta registrazione nuovo device + +

+ opzione x inviare email (data lista dipendenti) - solo x admin? +

+ +

+ opzione SOLO X ADMIN x resettare authKey dip + inviare email (data lista dipendenti) +

+

+ istruzioni x uso pagina jumper +

+ +@code { + +} diff --git a/GPW.CORE.SMART/Pages/_Layout.cshtml b/GPW.CORE.SMART/Pages/_Layout.cshtml index 7e1952c..d7b0acd 100644 --- a/GPW.CORE.SMART/Pages/_Layout.cshtml +++ b/GPW.CORE.SMART/Pages/_Layout.cshtml @@ -28,6 +28,8 @@ 🗙 + + diff --git a/GPW.CORE.SMART/Program.cs b/GPW.CORE.SMART/Program.cs index db0f9a4..22f1b66 100644 --- a/GPW.CORE.SMART/Program.cs +++ b/GPW.CORE.SMART/Program.cs @@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Identity.UI.Services; using GPW.CORE.Data; using Blazored.LocalStorage; +using Blazored.SessionStorage; var builder = WebApplication.CreateBuilder(args); @@ -37,6 +38,7 @@ builder.Services.AddSingleton(redisMultiplexer); builder.Services.AddScoped(); builder.Services.AddBlazoredLocalStorage(); +builder.Services.AddBlazoredSessionStorage(); var app = builder.Build(); diff --git a/GPW.CORE.SMART/wwwroot/lib/UserAgent.js b/GPW.CORE.SMART/wwwroot/lib/UserAgent.js new file mode 100644 index 0000000..55ca331 --- /dev/null +++ b/GPW.CORE.SMART/wwwroot/lib/UserAgent.js @@ -0,0 +1,3 @@ +window.getUserAgent = () => { + return navigator.userAgent; +}; diff --git a/GPW.CORE.SMART/wwwroot/lib/WindowSize.js b/GPW.CORE.SMART/wwwroot/lib/WindowSize.js new file mode 100644 index 0000000..a8d2d77 --- /dev/null +++ b/GPW.CORE.SMART/wwwroot/lib/WindowSize.js @@ -0,0 +1,6 @@ +window.getWindowDimensions = function () { + return { + width: window.innerWidth, + height: window.innerHeight + }; +}; \ No newline at end of file