diff --git a/StandManager/Components/Account/IdentityRedirectManager.cs b/StandManager/Components/Account/IdentityRedirectManager.cs deleted file mode 100644 index 693d077..0000000 --- a/StandManager/Components/Account/IdentityRedirectManager.cs +++ /dev/null @@ -1,59 +0,0 @@ -using Microsoft.AspNetCore.Components; -using System.Diagnostics.CodeAnalysis; - -namespace StandManager.Components.Account -{ - internal sealed class IdentityRedirectManager(NavigationManager navigationManager) - { - public const string StatusCookieName = "Identity.StatusMessage"; - - private static readonly CookieBuilder StatusCookieBuilder = new() - { - SameSite = SameSiteMode.Strict, - HttpOnly = true, - IsEssential = true, - MaxAge = TimeSpan.FromSeconds(5), - }; - - [DoesNotReturn] - public void RedirectTo(string? uri) - { - uri ??= ""; - - // Prevent open redirects. - if (!Uri.IsWellFormedUriString(uri, UriKind.Relative)) - { - uri = navigationManager.ToBaseRelativePath(uri); - } - - // During static rendering, NavigateTo throws a NavigationException which is handled by the framework as a redirect. - // So as long as this is called from a statically rendered Identity component, the InvalidOperationException is never thrown. - navigationManager.NavigateTo(uri); - throw new InvalidOperationException($"{nameof(IdentityRedirectManager)} can only be used during static rendering."); - } - - [DoesNotReturn] - public void RedirectTo(string uri, Dictionary queryParameters) - { - var uriWithoutQuery = navigationManager.ToAbsoluteUri(uri).GetLeftPart(UriPartial.Path); - var newUri = navigationManager.GetUriWithQueryParameters(uriWithoutQuery, queryParameters); - RedirectTo(newUri); - } - - [DoesNotReturn] - public void RedirectToWithStatus(string uri, string message, HttpContext context) - { - context.Response.Cookies.Append(StatusCookieName, message, StatusCookieBuilder.Build(context)); - RedirectTo(uri); - } - - private string CurrentPath => navigationManager.ToAbsoluteUri(navigationManager.Uri).GetLeftPart(UriPartial.Path); - - [DoesNotReturn] - public void RedirectToCurrentPage() => RedirectTo(CurrentPath); - - [DoesNotReturn] - public void RedirectToCurrentPageWithStatus(string message, HttpContext context) - => RedirectToWithStatus(CurrentPath, message, context); - } -} diff --git a/StandManager/Components/Account/IdentityRevalidatingAuthenticationStateProvider.cs b/StandManager/Components/Account/IdentityRevalidatingAuthenticationStateProvider.cs deleted file mode 100644 index a9f3e5f..0000000 --- a/StandManager/Components/Account/IdentityRevalidatingAuthenticationStateProvider.cs +++ /dev/null @@ -1,46 +0,0 @@ -using Microsoft.AspNetCore.Components.Authorization; -using Microsoft.AspNetCore.Components.Server; -using Microsoft.AspNetCore.Identity; -using Microsoft.Extensions.Options; -using StandManager.Domain.Entita; -using System.Security.Claims; - -namespace StandManager.Components.Account -{ - internal sealed class IdentityRevalidatingAuthenticationStateProvider( - ILoggerFactory loggerFactory, - IServiceScopeFactory scopeFactory, - IOptions options) - : RevalidatingServerAuthenticationStateProvider(loggerFactory) - { - protected override TimeSpan RevalidationInterval => TimeSpan.FromMinutes(30); - - protected override async Task ValidateAuthenticationStateAsync( - AuthenticationState authenticationState, CancellationToken cancellationToken) - { - // Get the user manager from a new scope to ensure it fetches fresh data - await using var scope = scopeFactory.CreateAsyncScope(); - var userManager = scope.ServiceProvider.GetRequiredService>(); - return await ValidateSecurityStampAsync(userManager, authenticationState.User); - } - - private async Task ValidateSecurityStampAsync(UserManager userManager, ClaimsPrincipal principal) - { - var user = await userManager.GetUserAsync(principal); - if (user is null) - { - return false; - } - else if (!userManager.SupportsUserSecurityStamp) - { - return true; - } - else - { - var principalStamp = principal.FindFirstValue(options.Value.ClaimsIdentity.SecurityStampClaimType); - var userStamp = await userManager.GetSecurityStampAsync(user); - return principalStamp == userStamp; - } - } - } -} diff --git a/StandManager/Components/Account/IdentityUserAccessor.cs b/StandManager/Components/Account/IdentityUserAccessor.cs deleted file mode 100644 index d7f0838..0000000 --- a/StandManager/Components/Account/IdentityUserAccessor.cs +++ /dev/null @@ -1,20 +0,0 @@ -using Microsoft.AspNetCore.Identity; -using StandManager.Domain.Entita; - -namespace StandManager.Components.Account -{ - internal sealed class IdentityUserAccessor(UserManager userManager, IdentityRedirectManager redirectManager) - { - public async Task GetRequiredUserAsync(HttpContext context) - { - var user = await userManager.GetUserAsync(context.User); - - if (user is null) - { - redirectManager.RedirectToWithStatus("Account/InvalidUser", $"Error: Unable to load user with ID '{userManager.GetUserId(context.User)}'.", context); - } - - return user; - } - } -} diff --git a/StandManager/Components/Layout/MainLayout.razor b/StandManager/Components/Layout/MainLayout.razor index 6e6f901..6e5ab13 100644 --- a/StandManager/Components/Layout/MainLayout.razor +++ b/StandManager/Components/Layout/MainLayout.razor @@ -9,7 +9,7 @@

- +

diff --git a/StandManager/Components/Pages/Auth.razor b/StandManager/Components/Pages/Auth.razor deleted file mode 100644 index b7bbe6e..0000000 --- a/StandManager/Components/Pages/Auth.razor +++ /dev/null @@ -1,13 +0,0 @@ -@page "/auth" - -@using Microsoft.AspNetCore.Authorization - -@attribute [Authorize] - -Auth - -

You are authenticated

- - - Hello @context.User.Identity?.Name! - diff --git a/StandManager/Components/Pages/Management/Login.razor b/StandManager/Components/Pages/Management/Login.razor deleted file mode 100644 index 95d6b92..0000000 --- a/StandManager/Components/Pages/Management/Login.razor +++ /dev/null @@ -1,89 +0,0 @@ -@rendermode InteractiveServer -@layout PublicLayout -@page "/management/login" -@using System.Security.Claims -@using Microsoft.AspNetCore.Authentication -@using Microsoft.AspNetCore.Identity -@using OAService.Domain.Entita -@using StandManager.Components.Layout -@using StandManager.Domain.Entita -@using StandManager.Model -@using StandManager.Service.Interfaces -@using Microsoft.AspNetCore.Http -@inject NavigationManager Navigation -@inject IHttpContextAccessor HttpContextAccessor -@inject HttpClient Http -@inject UserManager UserManager -@inject IUserStore UserStore -@inject SignInManager SignInManager - -
-
-
-
-
-
-

Accedi

- - - -
- - - -
- -
- - - -
- - - - @if (!string.IsNullOrEmpty(errorMessage)) - { -
@errorMessage
- } -
-
-
-
-
- -@code { - [SupplyParameterFromForm] - private LoginModel? model { get; set; } - - private string? errorMessage; - protected override void OnInitialized() => model ??= new(); - private async Task HandleValidSubmit() - { - var u = UserManager.Users.FirstOrDefault(); - var result = await SignInManager.CheckPasswordSignInAsync(u, "test123pwd@", lockoutOnFailure: false); - - if (result.Succeeded) - { - await SignInManager.SignInAsync(u, isPersistent: true, "standmanager"); - } - Navigation.NavigateTo("management/Dashboard"); - // var response = await Http.PostAsJsonAsync("/management/adminLogin", model); - - // if (!response.IsSuccessStatusCode) - // { - // // leggiamo il messaggio dall'endpoint (opzionale) - // var msg = await response.Content.ReadAsStringAsync(); - // errorMessage = string.IsNullOrWhiteSpace(msg) - // ? "Credenziali non valide." - // : msg; - - // return; - // } - // var u = await _managerService.UtenteService.RicercaPer(x => x.Email == model!.Email); - // await SignInManager.SignInAsync(new ApplicationUser(), new AuthenticationProperties { IsPersistent = false }); - // // Login ok → vai al management - // Navigation.NavigateTo("management/Dashboard"); - } -} \ No newline at end of file diff --git a/StandManager/Components/Pages/Management/PublicHeader.razor b/StandManager/Components/Pages/Management/PublicHeader.razor deleted file mode 100644 index f652f2f..0000000 --- a/StandManager/Components/Pages/Management/PublicHeader.razor +++ /dev/null @@ -1,16 +0,0 @@ -@rendermode InteractiveServer - -

Questo è il layout pubblico

- - -

@message

- -@code { - private string message = "In attesa di saluto…"; - - private void SayHi() - { - message = $"Ciao dal layout pubblico alle {DateTime.Now:T}"; - Console.WriteLine("Ciao dal layout pubblico"); // log server - } -} \ No newline at end of file diff --git a/StandManager/Program.cs b/StandManager/Program.cs index 6813bc6..6fcf026 100644 --- a/StandManager/Program.cs +++ b/StandManager/Program.cs @@ -1,20 +1,14 @@ -using Microsoft.AspNetCore.Authentication; -using Microsoft.AspNetCore.Authentication.Cookies; -using Microsoft.AspNetCore.Components.Authorization; +using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Radzen; using StandManager.Components; -using StandManager.Components.Account; -using StandManager.Domain.Entita; using StandManager.Infrastructure.DAL.Context; -using StandManager.Model; using StandManager.Service; using StandManager.Service.Interfaces; using StandManager.Service.Repository; using StandManager.Utils; using System.Reflection; -using System.Security.Claims; var builder = WebApplication.CreateBuilder(args); @@ -23,9 +17,6 @@ builder.Services.AddRazorComponents() .AddInteractiveServerComponents(); builder.Services.AddCascadingAuthenticationState(); -//builder.Services.AddScoped(); -//builder.Services.AddScoped(); -//builder.Services.AddScoped(); builder.Services.Configure(options => { @@ -65,6 +56,7 @@ builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationSc options.Cookie.Name = "auth_token"; options.Cookie.MaxAge = TimeSpan.FromMinutes(30); options.LoginPath = "/account/login"; + options.LogoutPath = "/account/logout"; options.AccessDeniedPath = "/access-denied"; }); builder.Services.AddAuthorization(); diff --git a/StandManager/StandManager.csproj b/StandManager/StandManager.csproj index 872101b..be52cf3 100644 --- a/StandManager/StandManager.csproj +++ b/StandManager/StandManager.csproj @@ -27,47 +27,6 @@ - - <_ContentIncludedByDefault Remove="Components\Account\Pages\AccessDenied.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\ConfirmEmail.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\ConfirmEmailChange.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\ExternalLogin.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\ForgotPassword.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\ForgotPasswordConfirmation.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\InvalidPasswordReset.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\InvalidUser.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\Lockout.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\Login.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\LoginWith2fa.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\LoginWithRecoveryCode.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\Manage\ChangePassword.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\Manage\DeletePersonalData.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\Manage\Disable2fa.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\Manage\Email.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\Manage\EnableAuthenticator.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\Manage\ExternalLogins.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\Manage\GenerateRecoveryCodes.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\Manage\Index.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\Manage\PersonalData.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\Manage\ResetAuthenticator.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\Manage\SetPassword.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\Manage\TwoFactorAuthentication.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\Manage\_Imports.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\Register.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\RegisterConfirmation.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\ResendEmailConfirmation.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\ResetPassword.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\ResetPasswordConfirmation.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Pages\_Imports.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Shared\AccountLayout.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Shared\ExternalLoginPicker.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Shared\ManageLayout.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Shared\ManageNavMenu.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Shared\RedirectToLogin.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Shared\ShowRecoveryCodes.razor" /> - <_ContentIncludedByDefault Remove="Components\Account\Shared\StatusMessage.razor" /> - - ..\Libs\OAService.Domain.dll