From 111f47d273cc8da30b210b1de6f769edfc7db06a Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Mon, 20 Feb 2023 15:35:59 +0100 Subject: [PATCH] Aggiunta gestione preliminare AUTH --- StockMan.CORE/App.razor | 26 +++-- StockMan.CORE/Components/CmpTop.razor | 17 +++ StockMan.CORE/Components/CmpTop.razor.cs | 79 +++++++++++++ StockMan.CORE/Components/NewNavMenu.razor | 7 +- StockMan.CORE/Components/NewNavMenu.razor.cs | 114 ++++++++++++------- StockMan.CORE/Program.cs | 19 ++-- StockMan.CORE/Properties/launchSettings.json | 4 +- StockMan.CORE/Shared/MainLayout.razor | 2 +- StockMan.CORE/StockMan.CORE.csproj | 3 +- 9 files changed, 204 insertions(+), 67 deletions(-) create mode 100644 StockMan.CORE/Components/CmpTop.razor create mode 100644 StockMan.CORE/Components/CmpTop.razor.cs diff --git a/StockMan.CORE/App.razor b/StockMan.CORE/App.razor index 6fd3ed1..ddf97f7 100644 --- a/StockMan.CORE/App.razor +++ b/StockMan.CORE/App.razor @@ -1,12 +1,14 @@ - - - - - - - Not found - -

Sorry, there's nothing at this address.

-
-
-
+ + + + + + + + Not found + +

Sorry, there's nothing at this address.

+
+
+
+
\ No newline at end of file diff --git a/StockMan.CORE/Components/CmpTop.razor b/StockMan.CORE/Components/CmpTop.razor new file mode 100644 index 0000000..d322783 --- /dev/null +++ b/StockMan.CORE/Components/CmpTop.razor @@ -0,0 +1,17 @@ +@using System.Security.Claims +@using Microsoft.AspNetCore.Components.Authorization + +@inject AuthenticationStateProvider AuthenticationStateProvider + +
+
+ + @userName + +
+
+ +
+
+ + diff --git a/StockMan.CORE/Components/CmpTop.razor.cs b/StockMan.CORE/Components/CmpTop.razor.cs new file mode 100644 index 0000000..f6ecfa6 --- /dev/null +++ b/StockMan.CORE/Components/CmpTop.razor.cs @@ -0,0 +1,79 @@ +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Authorization; +using Microsoft.JSInterop; +using StockMan.CORE.Data; + +namespace StockMan.CORE.Components +{ + public partial class CmpTop + { + #region Public Methods + + public async Task flushCache() + { + await Task.Delay(1); + await SDService.FlushRedisCache(); + await Task.Delay(1); + // rimando a pagina corrente + NavManager.NavigateTo(NavManager.Uri, true); + } + + #endregion Public Methods + + #region Protected Properties + + [Inject] + protected IJSRuntime JSRuntime { get; set; } = null!; + + [Inject] + protected StockDataService SDService { get; set; } = null!; + + #endregion Protected Properties + + #region Protected Methods + + protected override async Task OnInitializedAsync() + { + await forceReload(); + } + + #endregion Protected Methods + + #region Private Fields + + private string userName = ""; + + #endregion Private Fields + + #region Private Properties + + [Inject] + private NavigationManager NavManager { get; set; } = null!; + + #endregion Private Properties + + #region Private Methods + + private async Task forceReload() + { + var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); + var user = authState.User; + if (user.Identity != null && user.Identity.IsAuthenticated) + { + userName = $"{user.Identity.Name}"; + } + else + { + userName = "N.A."; + } + } + + private async void MService_EA_ShowSearch() + { + await Task.Delay(1); + await InvokeAsync(() => StateHasChanged()); + } + + #endregion Private Methods + } +} \ No newline at end of file diff --git a/StockMan.CORE/Components/NewNavMenu.razor b/StockMan.CORE/Components/NewNavMenu.razor index 250436f..e316524 100644 --- a/StockMan.CORE/Components/NewNavMenu.razor +++ b/StockMan.CORE/Components/NewNavMenu.razor @@ -91,7 +91,10 @@
- @if (!isComp) + + @userName + + @* @if (!isComp) { @if (User.Contains("/")) { @@ -123,7 +126,7 @@ @($"{User.Substring(0, 1).ToUpper()}")
} - } + }*@
diff --git a/StockMan.CORE/Components/NewNavMenu.razor.cs b/StockMan.CORE/Components/NewNavMenu.razor.cs index 4aac68f..4f6f169 100644 --- a/StockMan.CORE/Components/NewNavMenu.razor.cs +++ b/StockMan.CORE/Components/NewNavMenu.razor.cs @@ -1,53 +1,61 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using Microsoft.AspNetCore.Components; -using System.Net.Http; -using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Components.Authorization; -using Microsoft.AspNetCore.Components.Forms; -using Microsoft.AspNetCore.Components.Routing; -using Microsoft.AspNetCore.Components.Web; -using Microsoft.AspNetCore.Components.Web.Virtualization; using Microsoft.JSInterop; -using StockMan.CORE; -using StockMan.CORE.Shared; -using StockMan.CORE.Data; using StockMan.Data.DbModels; -using Microsoft.Extensions.Hosting; -using System.Diagnostics.Contracts; namespace StockMan.CORE.Components { public partial class NewNavMenu { + #region Public Properties + [Parameter] public List? NavMenuLinks { get; set; } = null; + public List NavMenuLinksChild { get; set; } = new List(); + + [Parameter] + public string Theme { get; set; } = ""; + [Parameter] public string Title { get; set; } = ""; - [Parameter] - public string User { get; set; } = ""; + #endregion Public Properties - [Parameter] - public string Theme { get; set; } = ""; + #region Protected Fields + + protected bool isChildOpen = false; + protected bool isComp = false; + + #endregion Protected Fields + + #region Protected Properties + + [Inject] + protected AuthenticationStateProvider AuthenticationStateProvider { get; set; } = null!; + + protected int currChildOpen { get; set; } = 0; [Inject] protected IJSRuntime JSRuntime { get; set; } = null!; - protected bool isComp = false; - protected bool isChildOpen = false; - public List NavMenuLinksChild { get; set; } = new List(); + #endregion Protected Properties - protected int currChildOpen { get; set; } = 0; + #region Protected Methods protected void compress() { JSRuntime.InvokeVoidAsync("closeNav"); isComp = true; } + + protected void deCompress() + { + JSRuntime.InvokeVoidAsync("openNav"); + JSRuntime.InvokeVoidAsync("showMenuBtn"); + isComp = false; + } + protected void doShowChildren(int id) { if (currChildOpen != 0) @@ -82,26 +90,10 @@ namespace StockMan.CORE.Components else { JSRuntime.InvokeVoidAsync("doHideChildren", id); - currChildOpen= 0; + currChildOpen = 0; } } - protected string isActive(int gruppo) - { - string answ = "navLinkItem"; - if (gruppo == currChildOpen) - { - answ = "navLinkItemActive"; - } - return answ; - } - - protected void deCompress() - { - JSRuntime.InvokeVoidAsync("openNav"); - JSRuntime.InvokeVoidAsync("showMenuBtn"); - isComp = false; - } protected bool hasChild(int group) { bool hasIt = false; @@ -115,5 +107,47 @@ namespace StockMan.CORE.Components } return hasIt; } + + protected string isActive(int gruppo) + { + string answ = "navLinkItem"; + if (gruppo == currChildOpen) + { + answ = "navLinkItemActive"; + } + return answ; + } + + protected override async Task OnInitializedAsync() + { + await forceReload(); + } + + #endregion Protected Methods + + #region Private Fields + + private string userName = ""; + + #endregion Private Fields + + #region Private Methods + + private async Task forceReload() + { + var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); + var user = authState.User; + if (user.Identity != null && user.Identity.IsAuthenticated) + { + userName = $"{user.Identity.Name}"; + // cercare in tabella operators e salvare sigla da registrare nei vari records sul DB + } + else + { + userName = "N.A."; + } + } + + #endregion Private Methods } } \ No newline at end of file diff --git a/StockMan.CORE/Program.cs b/StockMan.CORE/Program.cs index 4471631..3f863e0 100644 --- a/StockMan.CORE/Program.cs +++ b/StockMan.CORE/Program.cs @@ -8,17 +8,18 @@ using StockMan.Data; using System.Globalization; using Blazored.LocalStorage; using Blazored.SessionStorage; +using Microsoft.AspNetCore.Authentication.Negotiate; var builder = WebApplication.CreateBuilder(args); -//builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme) -// .AddNegotiate(); +builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme) + .AddNegotiate(); -//builder.Services.AddAuthorization(options => -//{ -// // By default, all incoming requests will be authorized according to the default policy. -// options.FallbackPolicy = options.DefaultPolicy; -//}); +builder.Services.AddAuthorization(options => +{ + // By default, all incoming requests will be authorized according to the default policy. + options.FallbackPolicy = options.DefaultPolicy; +}); // configuration setup ConfigurationManager configuration = builder.Configuration; @@ -80,8 +81,8 @@ app.UseStaticFiles(); app.UseRouting(); -//app.UseAuthentication(); -//app.UseAuthorization(); +app.UseAuthentication(); +app.UseAuthorization(); app.MapBlazorHub(); app.MapFallbackToPage("/_Host"); diff --git a/StockMan.CORE/Properties/launchSettings.json b/StockMan.CORE/Properties/launchSettings.json index 45d304c..21d09be 100644 --- a/StockMan.CORE/Properties/launchSettings.json +++ b/StockMan.CORE/Properties/launchSettings.json @@ -1,7 +1,7 @@ { "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, + "windowsAuthentication": true, + "anonymousAuthentication": false, "iisExpress": { "applicationUrl": "http://localhost:15225", "sslPort": 44328 diff --git a/StockMan.CORE/Shared/MainLayout.razor b/StockMan.CORE/Shared/MainLayout.razor index e62fd15..e2907e1 100644 --- a/StockMan.CORE/Shared/MainLayout.razor +++ b/StockMan.CORE/Shared/MainLayout.razor @@ -5,7 +5,7 @@
diff --git a/StockMan.CORE/StockMan.CORE.csproj b/StockMan.CORE/StockMan.CORE.csproj index af9bff2..af96d37 100644 --- a/StockMan.CORE/StockMan.CORE.csproj +++ b/StockMan.CORE/StockMan.CORE.csproj @@ -19,7 +19,8 @@ - + +