184 lines
6.0 KiB
C#
184 lines
6.0 KiB
C#
using EgwCoreLib.Razor.Data;
|
|
using EgwCoreLib.Utils;
|
|
using GPW.CORE.Data.DbModels;
|
|
using GPW.CORE.Smart.Data;
|
|
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.AspNetCore.WebUtilities;
|
|
using Microsoft.JSInterop;
|
|
using NLog;
|
|
|
|
namespace GPW.CORE.Smart.Components.Pages
|
|
{
|
|
public partial class Jumper
|
|
{
|
|
#region Public Classes
|
|
|
|
public class WindowDimension
|
|
{
|
|
#region Public Properties
|
|
|
|
public int Height { get; set; }
|
|
public int Width { get; set; }
|
|
|
|
#endregion Public Properties
|
|
}
|
|
|
|
#endregion Public Classes
|
|
|
|
#region Protected Fields
|
|
|
|
protected DipendentiModel? currDip;
|
|
|
|
protected List<DipendentiModel>? currDipList;
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Protected Properties
|
|
|
|
protected string authKey { get; set; } = "";
|
|
|
|
protected int idxDipendente = 0;
|
|
|
|
#endregion Protected Properties
|
|
|
|
#region Protected Methods
|
|
|
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
|
{
|
|
await Task.Delay(50);
|
|
if (firstRender)
|
|
{
|
|
await tryUrlLogin();
|
|
}
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Private Fields
|
|
|
|
private Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
private Random rnd = new Random();
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Properties
|
|
|
|
[Inject]
|
|
private CoreSmartDataService CDService { get; set; } = null!;
|
|
|
|
[Inject]
|
|
private IHttpContextAccessor httpContextAccessor { get; set; } = null!;
|
|
|
|
[Inject]
|
|
private IJSRuntime JSRuntime { get; set; } = null!;
|
|
|
|
[Inject]
|
|
private MessageService MService { get; set; } = null!;
|
|
|
|
[Inject]
|
|
private NavigationManager navManager { get; set; } = null!;
|
|
|
|
#endregion Private Properties
|
|
|
|
#region Private Methods
|
|
|
|
/// <summary>
|
|
/// Prova login da dati URL
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private async Task tryUrlLogin()
|
|
{
|
|
var uri = navManager.ToAbsoluteUri(navManager.Uri);
|
|
await Task.Delay(1);
|
|
// resetto valori in localStorage...
|
|
string devSec = "";
|
|
string devIp = "";
|
|
authKey = "";
|
|
await MService.setDevSecretAsync(devSec);
|
|
await MService.setDevIpv4Async(devIp);
|
|
// recupero da URL
|
|
if (QueryHelpers.ParseQuery(uri.Query).TryGetValue("idxDipendente", out var _idxDipendente))
|
|
{
|
|
if (!string.IsNullOrEmpty(_idxDipendente))
|
|
{
|
|
idxDipendente = int.Parse($"{_idxDipendente}");
|
|
}
|
|
}
|
|
if (QueryHelpers.ParseQuery(uri.Query).TryGetValue("authKey", out var _authKey))
|
|
{
|
|
if (!string.IsNullOrEmpty(_authKey))
|
|
{
|
|
authKey = $"{_authKey}";
|
|
}
|
|
}
|
|
else if (QueryHelpers.ParseQuery(uri.Query).TryGetValue("UserAuthKey", out var _userAuthKey))
|
|
{
|
|
if (!string.IsNullOrEmpty(_userAuthKey))
|
|
{
|
|
authKey = $"{_userAuthKey}";
|
|
}
|
|
}
|
|
DateTime adesso = DateTime.Now;
|
|
string dtData = $"{DateTime.UtcNow:yyyy-MM-dd_ZHH:mm:ss}";
|
|
string devAgent = await JSRuntime.InvokeAsync<string>("getUserAgent");
|
|
var dimension = await JSRuntime.InvokeAsync<WindowDimension>("getWindowDimensions");
|
|
string screenSize = $"{dimension.Width}x{dimension.Height}";
|
|
if (httpContextAccessor.HttpContext != null)
|
|
{
|
|
var remoteIp = $"{httpContextAccessor.HttpContext.Connection?.RemoteIpAddress}";
|
|
// provo a recuperare ipV4...
|
|
devIp = IpUtils.getLocalIpv4(remoteIp);
|
|
}
|
|
string rndTxt01 = SteamCrypto.RandomString(16, true);
|
|
string rndTxt02 = SteamCrypto.RandomString(16, true);
|
|
currDipList = await CDService.DipendentiGetAll();
|
|
currDip = currDipList.Where(x => x.IdxDipendente == idxDipendente && x.AuthKey == authKey).FirstOrDefault();
|
|
if (currDip != null)
|
|
{
|
|
// verifico authKey...
|
|
if (currDip.AuthKey == authKey)
|
|
{
|
|
// creo la DeviceSecret
|
|
devSec = CDService.EncriptData($"{rndTxt01}|{dtData}|{idxDipendente}|{devIp}|{rndTxt02}");
|
|
// ...e la salvo sul DB
|
|
AnagDeviceModel newDev = new AnagDeviceModel()
|
|
{
|
|
IdxDipendente = idxDipendente,
|
|
DeviceSecret = devSec,
|
|
DataOraEnabled = adesso,
|
|
DataOraLastSeen = adesso,
|
|
ScreenSize = screenSize,
|
|
DeviceName = devIp,
|
|
Description = devAgent,
|
|
LastIpv4 = devIp
|
|
};
|
|
bool fatto = await CDService.DeviceInsert(newDev);
|
|
if (fatto)
|
|
{
|
|
// ...e nel browser tramite message service
|
|
await MService.setDevSecretAsync(devSec);
|
|
await MService.setDevIpv4Async(devIp);
|
|
// infine salvo dati dipendente e rimando a login
|
|
MService.RigaDip = currDip;
|
|
navManager.NavigateTo($"{baseUrl}/Home", true);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
navManager.NavigateTo($"{baseUrl}/RegNewDevice");
|
|
}
|
|
}
|
|
}
|
|
|
|
protected string baseUrl
|
|
{
|
|
get => configuration.GetValue<string>("OptConf:BaseUrl") ?? "~/";
|
|
}
|
|
|
|
[Inject]
|
|
protected IConfiguration configuration { get; set; } = null!;
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |