Membership
This commit is contained in:
59
StandManager/Components/Account/IdentityRedirectManager.cs
Normal file
59
StandManager/Components/Account/IdentityRedirectManager.cs
Normal file
@ -0,0 +1,59 @@
|
||||
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<string, object?> 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);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,46 @@
|
||||
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<IdentityOptions> options)
|
||||
: RevalidatingServerAuthenticationStateProvider(loggerFactory)
|
||||
{
|
||||
protected override TimeSpan RevalidationInterval => TimeSpan.FromMinutes(30);
|
||||
|
||||
protected override async Task<bool> 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<UserManager<ApplicationUser>>();
|
||||
return await ValidateSecurityStampAsync(userManager, authenticationState.User);
|
||||
}
|
||||
|
||||
private async Task<bool> ValidateSecurityStampAsync(UserManager<ApplicationUser> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
20
StandManager/Components/Account/IdentityUserAccessor.cs
Normal file
20
StandManager/Components/Account/IdentityUserAccessor.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using StandManager.Domain.Entita;
|
||||
|
||||
namespace StandManager.Components.Account
|
||||
{
|
||||
internal sealed class IdentityUserAccessor(UserManager<ApplicationUser> userManager, IdentityRedirectManager redirectManager)
|
||||
{
|
||||
public async Task<ApplicationUser> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -3,9 +3,9 @@
|
||||
|
||||
<head>
|
||||
<base href="/" />
|
||||
<meta charset="utf-8"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover"/>
|
||||
<meta http-equiv="X-UA-Compatible" content="ie=edge"/>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
|
||||
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
|
||||
<link href="/lib/tabler/tabler.min.css" rel="stylesheet" />
|
||||
<link href="/css/tabler-flags.css" rel="stylesheet" />
|
||||
<link href="/css/tabler-socials.css" rel="stylesheet" />
|
||||
@ -17,14 +17,16 @@
|
||||
<link href="/libs/Fontawesome/css/all.min.css" rel="stylesheet" />
|
||||
<link href="/libs/Fontawesome/css/regular.min.css" rel="stylesheet" />
|
||||
<link rel="icon" type="image/png" href="favicon.png" />
|
||||
<HeadOutlet/>
|
||||
<HeadOutlet />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<Routes/>
|
||||
<div class="page">
|
||||
<Routes />
|
||||
</div>
|
||||
|
||||
<script src="_framework/blazor.web.js"></script>
|
||||
|
||||
|
||||
<script src="/lib/jquery/dist/jquery.js" asp-append-version="true"></script>
|
||||
<script src="/lib/tabler/tabler.min.js" asp-append-version="true"></script>
|
||||
<script src="/libs/FontAwesome/js/all.min.js" asp-append-version="true"></script>
|
||||
|
||||
@ -1,76 +1,79 @@
|
||||
@using Microsoft.AspNetCore.Authentication
|
||||
@inherits LayoutComponentBase
|
||||
|
||||
|
||||
<body class="body-marketing body-gradient">
|
||||
|
||||
<div class="page">
|
||||
<!-- NAV MENU MANAGEMENT -->
|
||||
<header class="navbar navbar-expand-md d-print-none" >
|
||||
<div class="container-xl">
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbar-menu" aria-controls="navbar-menu" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<h1 class="navbar-brand navbar-brand-autodark d-none-navbar-horizontal pe-0 pe-md-3">
|
||||
<a href="/management">
|
||||
<img src="/Logo_dac.png" width="80" class="">
|
||||
</a>
|
||||
</h1>
|
||||
<AuthorizeView>
|
||||
<Authorized>
|
||||
<div class="navbar-nav flex-row order-md-last">
|
||||
<div class="nav-item dropdown">
|
||||
<a href="#" class="nav-link d-flex lh-1 text-reset p-0" data-bs-toggle="dropdown" aria-label="Open user menu">
|
||||
<span class="avatar avatar-sm" style="background-image: url('/Logo_dac.png')"></span>
|
||||
<div class="d-none d-xl-block ps-2">
|
||||
<div>@context.User.Identity?.Name</div>
|
||||
</div>
|
||||
</a>
|
||||
<div class="dropdown-menu dropdown-menu-end dropdown-menu-arrow">
|
||||
<a href="/management/profile" class="dropdown-item">Il mio profilo</a>
|
||||
<div class="dropdown-divider"></div>
|
||||
<button class="dropdown-item" @onclick="OnLogoutPressed">Settings</button>
|
||||
<a @onclick="OnLogoutPressed" class="dropdown-item">Logout @context.User.Identity?.Name 1</a>
|
||||
<div class="page">
|
||||
<!-- NAV MENU MANAGEMENT -->
|
||||
<header class="navbar navbar-expand-md d-print-none" >
|
||||
<div class="container-xl">
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbar-menu" aria-controls="navbar-menu" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<h1 class="navbar-brand navbar-brand-autodark d-none-navbar-horizontal pe-0 pe-md-3">
|
||||
<a href="/management">
|
||||
<img src="/Logo_dac.png" width="80" class="">
|
||||
</a>
|
||||
</h1>
|
||||
<AuthorizeView>
|
||||
<Authorized>
|
||||
<div class="navbar-nav flex-row order-md-last">
|
||||
<div class="nav-item dropdown">
|
||||
<a href="#" class="nav-link d-flex lh-1 text-reset p-0" data-bs-toggle="dropdown" aria-label="Open user menu">
|
||||
<span class="avatar avatar-sm" style="background-image: url('/Logo_dac.png')"></span>
|
||||
<div class="d-none d-xl-block ps-2">
|
||||
<div>@context.User.Identity?.Name</div>
|
||||
</div>
|
||||
</a>
|
||||
<div class="dropdown-menu dropdown-menu-end dropdown-menu-arrow">
|
||||
<a href="/management/profile" class="dropdown-item">Il mio profilo</a>
|
||||
<div class="dropdown-divider"></div>
|
||||
<a href="/account/logout" class="dropdown-item"> Logout </a>
|
||||
</div>
|
||||
</div>
|
||||
</Authorized>
|
||||
</AuthorizeView>
|
||||
</div>
|
||||
</header>
|
||||
<header class="navbar-expand-md">
|
||||
<div class="collapse navbar-collapse" id="navbar-menu">
|
||||
<div class="navbar">
|
||||
<div class="container-xl">
|
||||
<div class="row flex-column flex-md-row flex-fill align-items-center">
|
||||
<div class="col">
|
||||
<!-- BEGIN NAVBAR MENU -->
|
||||
<ul class="navbar-nav">
|
||||
<li class="nav-item active">
|
||||
<a class="nav-link" href="/management">
|
||||
<span class="nav-link-icon d-md-none d-lg-inline-block">
|
||||
<i class="fa-solid fa-house"></i>
|
||||
</span>
|
||||
<span class="nav-link-title"> Home </span>
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</Authorized>
|
||||
</AuthorizeView>
|
||||
</div>
|
||||
</header>
|
||||
<header class="navbar-expand-md">
|
||||
<div class="collapse navbar-collapse" id="navbar-menu">
|
||||
<div class="navbar">
|
||||
<div class="container-xl">
|
||||
<div class="row flex-column flex-md-row flex-fill align-items-center">
|
||||
<div class="col">
|
||||
<!-- BEGIN NAVBAR MENU -->
|
||||
<ul class="navbar-nav">
|
||||
<li class="nav-item active">
|
||||
<a class="nav-link" href="/">
|
||||
<span class="nav-link-icon d-md-none d-lg-inline-block">
|
||||
<i class="fa-solid fa-house"></i>
|
||||
</span>
|
||||
<span class="nav-link-title"> Home </span>
|
||||
</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="/management/dashboard">
|
||||
<span class="nav-link-icon d-md-none d-lg-inline-block">
|
||||
<i class="fa-solid fa-house"></i>
|
||||
</span>
|
||||
<span class="nav-link-title"> Dashboard </span>
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
</div>
|
||||
|
||||
@Body
|
||||
</body>
|
||||
</div>
|
||||
</header>
|
||||
</div>
|
||||
|
||||
@Body
|
||||
@code {
|
||||
[CascadingParameter] public HttpContext httpContext { get; set; } = default;
|
||||
|
||||
private async Task OnLogoutPressed(MouseEventArgs e)
|
||||
{
|
||||
var a = "Ciao";
|
||||
@* await httpContext.SignOutAsync(); *@
|
||||
}
|
||||
}
|
||||
90
StandManager/Components/Pages/Account/Login.razor
Normal file
90
StandManager/Components/Pages/Account/Login.razor
Normal file
@ -0,0 +1,90 @@
|
||||
@layout PublicLayout
|
||||
@page "/account/login"
|
||||
|
||||
@using Microsoft.AspNetCore.Authentication
|
||||
@using Microsoft.AspNetCore.Authentication.Cookies
|
||||
@using Microsoft.AspNetCore.Identity
|
||||
@using StandManager.Components.Layout
|
||||
@using StandManager.Domain.Entita
|
||||
@using StandManager.Infrastructure.DAL.Context
|
||||
@using StandManager.Model
|
||||
@using System.Security.Claims
|
||||
@using StandManager.Service.Interfaces
|
||||
|
||||
@inject StandManagerDbContext dbContext
|
||||
@inject NavigationManager navi
|
||||
@inject IHttpContextAccessor HttpContextAccessor
|
||||
@inject IManagerService _managerService
|
||||
|
||||
<div class="page page-center">
|
||||
<div class="container container-tight py-4">
|
||||
<div class="text-center mb-4">
|
||||
</div>
|
||||
<div class="card card-md">
|
||||
<div class="card-body">
|
||||
<h2 class="h2 text-center mb-4">Accedi</h2>
|
||||
<EditForm Model="model" OnValidSubmit="HandleValidSubmit" FormName="loginForm">
|
||||
<DataAnnotationsValidator />
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Email</label>
|
||||
<InputText class="form-control" @bind-Value="model!.Email" />
|
||||
<ValidationMessage For="@(() => model!.Email)" />
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Password</label>
|
||||
<InputText class="form-control" @bind-Value="model!.Password" type="password" />
|
||||
<ValidationMessage For="@(() => model!.Password)" />
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-primary w-100">
|
||||
Accedi
|
||||
</button>
|
||||
|
||||
@if (!string.IsNullOrEmpty(errorMessage))
|
||||
{
|
||||
<div class="alert alert-danger mt-3">@errorMessage</div>
|
||||
}
|
||||
</EditForm>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@code {
|
||||
[SupplyParameterFromForm]
|
||||
private LoginModel? model { get; set; }
|
||||
|
||||
private string? errorMessage { get; set; }
|
||||
|
||||
[CascadingParameter]
|
||||
public HttpContext? httpContext { get; set; }
|
||||
|
||||
protected override void OnInitialized() => model ??= new();
|
||||
|
||||
private async Task HandleValidSubmit(EditContext args)
|
||||
{
|
||||
var user = await _managerService.UtenteService.RicercaPer(x => x.Email == model.Email);
|
||||
var hasher = new PasswordHasher<Utente>();
|
||||
|
||||
if (user == null ||
|
||||
hasher.VerifyHashedPassword(user, user.Password, model.Password) != PasswordVerificationResult.Success)
|
||||
{
|
||||
// credenziali non valide
|
||||
errorMessage = "Credenziali non valide.";
|
||||
return;
|
||||
}
|
||||
|
||||
var claims = new List<Claim>
|
||||
{
|
||||
new Claim(ClaimTypes.Name, user.Email),
|
||||
new Claim(ClaimTypes.Role, "Admin")
|
||||
};
|
||||
|
||||
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
|
||||
var principal = new ClaimsPrincipal(identity);
|
||||
await httpContext.SignInAsync(principal);
|
||||
navi.NavigateTo("/management/dashboard");
|
||||
}
|
||||
}
|
||||
15
StandManager/Components/Pages/Account/Logout.razor
Normal file
15
StandManager/Components/Pages/Account/Logout.razor
Normal file
@ -0,0 +1,15 @@
|
||||
@page "/account/logout"
|
||||
@using Microsoft.AspNetCore.Authentication
|
||||
@inject NavigationManager navManager
|
||||
|
||||
@code {
|
||||
|
||||
[CascadingParameter]
|
||||
public HttpContext? httpContext { get; set; }
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
await httpContext.SignOutAsync();
|
||||
navManager.NavigateTo("/");
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,7 @@
|
||||
@page "/"
|
||||
@rendermode InteractiveServer
|
||||
@using Microsoft.AspNetCore.Identity
|
||||
@using StandManager.Domain.Entita
|
||||
|
||||
<PageTitle>Home</PageTitle>
|
||||
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
@page "/management"
|
||||
@page "/management/Dashboard"
|
||||
|
||||
@using Microsoft.AspNetCore.Authorization
|
||||
@using StandManager.Components.Layout
|
||||
@attribute [Authorize]
|
||||
@ -10,7 +11,9 @@
|
||||
<div class="container-xl">
|
||||
<div class="row g-2 align-items-center">
|
||||
<div class="col">
|
||||
<h2 class="page-title">Home</h2>
|
||||
<!-- Page pre-title -->
|
||||
<div class="page-pretitle">Overview</div>
|
||||
<h2 class="page-title">Dashboard</h2>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -18,5 +21,9 @@
|
||||
</div>
|
||||
|
||||
@code {
|
||||
|
||||
override protected void OnInitialized()
|
||||
{
|
||||
// Initialization logic can be added here if needed
|
||||
var a = "CIAO";
|
||||
}
|
||||
}
|
||||
@ -4,12 +4,18 @@
|
||||
@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
|
||||
@inject NavigationManager Nav
|
||||
@inject IManagerService _managerService
|
||||
@using Microsoft.AspNetCore.Http
|
||||
@inject NavigationManager Navigation
|
||||
@inject IHttpContextAccessor HttpContextAccessor
|
||||
@inject HttpClient Http
|
||||
@inject UserManager<ApplicationUser> UserManager
|
||||
@inject IUserStore<ApplicationUser> UserStore
|
||||
@inject SignInManager<ApplicationUser> SignInManager
|
||||
|
||||
<div class="page page-center">
|
||||
<div class="container container-tight py-4">
|
||||
@ -23,14 +29,14 @@
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Email</label>
|
||||
<InputText class="form-control" @bind-Value="model.Email"/>
|
||||
<ValidationMessage For="@(() => model.Email)"/>
|
||||
<InputText class="form-control" @bind-Value="model!.Email"/>
|
||||
<ValidationMessage For="@(() => model!.Email)"/>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Password</label>
|
||||
<InputText class="form-control" @bind-Value="model.Password" type="password"/>
|
||||
<ValidationMessage For="@(() => model.Password)"/>
|
||||
<InputText class="form-control" @bind-Value="model!.Password" type="password"/>
|
||||
<ValidationMessage For="@(() => model!.Password)"/>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-primary w-100">
|
||||
@ -48,38 +54,36 @@
|
||||
</div>
|
||||
|
||||
@code {
|
||||
[CascadingParameter] public HttpContext httpContext { get; set; } = default;
|
||||
|
||||
[SupplyParameterFromForm]
|
||||
private LoginModel model { get; set; }
|
||||
|
||||
private LoginModel? model { get; set; }
|
||||
|
||||
private string? errorMessage;
|
||||
protected override void OnInitialized() => model ??= new();
|
||||
private async Task HandleValidSubmit()
|
||||
{
|
||||
errorMessage = null;
|
||||
var u = UserManager.Users.FirstOrDefault();
|
||||
var result = await SignInManager.CheckPasswordSignInAsync(u, "test123pwd@", lockoutOnFailure: false);
|
||||
|
||||
var user = await _managerService.UtenteService.RicercaPer(x => x.Email == model.Email);
|
||||
var hasher = new PasswordHasher<Utente>();
|
||||
if (user == null || hasher.VerifyHashedPassword(user, user.Password, model.Password) != PasswordVerificationResult.Success)
|
||||
if (result.Succeeded)
|
||||
{
|
||||
errorMessage = "Credenziali non valide.";
|
||||
return;
|
||||
await SignInManager.SignInAsync(u, isPersistent: true, "standmanager");
|
||||
}
|
||||
|
||||
List<Claim> claims =
|
||||
[
|
||||
new(ClaimTypes.Name, user.Email),
|
||||
new(ClaimTypes.GivenName, user.Nome),
|
||||
new("Id", user.Id.ToString())
|
||||
];
|
||||
ClaimsIdentity identity = new(claims, "standmanager");
|
||||
ClaimsPrincipal claimsPrincipal = new(identity);
|
||||
await httpContext.SignInAsync("standmanager", claimsPrincipal, new AuthenticationProperties
|
||||
{
|
||||
IsPersistent = true,
|
||||
ExpiresUtc = DateTime.UtcNow.AddHours(8)
|
||||
});
|
||||
Nav.NavigateTo("/management");
|
||||
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");
|
||||
}
|
||||
}
|
||||
40
StandManager/LoginUtils.cs
Normal file
40
StandManager/LoginUtils.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using StandManager.Domain.Entita;
|
||||
using StandManager.Model;
|
||||
using StandManager.Service.Interfaces;
|
||||
|
||||
public static class LoginUtils
|
||||
{
|
||||
public static async Task<IResult> OnLogin(LoginModel model, IManagerService managerService, HttpContext ctx)
|
||||
{
|
||||
var user = await managerService.UtenteService.RicercaPer(x => x.Email == model.Email);
|
||||
var hasher = new PasswordHasher<Utente>();
|
||||
|
||||
if (user == null ||
|
||||
hasher.VerifyHashedPassword(user, user.Password, model.Password) != PasswordVerificationResult.Success)
|
||||
{
|
||||
// credenziali non valide
|
||||
return Results.BadRequest("Credenziali non valide.");
|
||||
}
|
||||
|
||||
List<Claim> claims =
|
||||
[
|
||||
new(ClaimTypes.Name, user.Email),
|
||||
new(ClaimTypes.GivenName, user.Nome),
|
||||
new("Id", user.Id.ToString())
|
||||
];
|
||||
|
||||
ClaimsIdentity identity = new(claims, "standmanager");
|
||||
ClaimsPrincipal claimsPrincipal = new(identity);
|
||||
|
||||
await ctx.SignInAsync("standmanager", claimsPrincipal, new AuthenticationProperties
|
||||
{
|
||||
IsPersistent = true,
|
||||
ExpiresUtc = DateTime.UtcNow.AddHours(8)
|
||||
});
|
||||
|
||||
return Results.Ok();
|
||||
}
|
||||
}
|
||||
@ -1,13 +1,20 @@
|
||||
using System.Reflection;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
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);
|
||||
|
||||
@ -16,12 +23,19 @@ builder.Services.AddRazorComponents()
|
||||
.AddInteractiveServerComponents();
|
||||
|
||||
builder.Services.AddCascadingAuthenticationState();
|
||||
//builder.Services.AddScoped<IdentityUserAccessor>();
|
||||
//builder.Services.AddScoped<IdentityRedirectManager>();
|
||||
//builder.Services.AddScoped<AuthenticationStateProvider, IdentityRevalidatingAuthenticationStateProvider>();
|
||||
|
||||
//Database
|
||||
var connectionString = builder.Configuration.GetConnectionString("ConnectionString");
|
||||
builder.Services.AddDbContext<StandManagerDbContext>(options =>
|
||||
options.UseSqlServer(connectionString)
|
||||
);
|
||||
builder.Services.Configure<IdentityOptions>(options =>
|
||||
{
|
||||
options.Password.RequireDigit = false;
|
||||
options.Password.RequireLowercase = false;
|
||||
options.Password.RequireUppercase = false;
|
||||
options.Password.RequireNonAlphanumeric = false;
|
||||
options.Password.RequiredLength = 1; // puoi metterla anche a 1
|
||||
options.Password.RequiredUniqueChars = 0;
|
||||
});
|
||||
|
||||
//DI
|
||||
var types = Assembly.Load("StandManager.Service").GetTypes();
|
||||
@ -31,32 +45,36 @@ foreach (var intfc in allProviderTypes.Where(t => t.IsInterface))
|
||||
var impl = allProviderTypes.FirstOrDefault(c => c.IsClass && intfc.Name[1..] == c.Name);
|
||||
if (impl != null) builder.Services.AddScoped(intfc, impl);
|
||||
}
|
||||
//Database
|
||||
var connectionString = builder.Configuration.GetConnectionString("ConnectionString");
|
||||
builder.Services.AddDbContext<StandManagerDbContext>(options =>
|
||||
options.UseSqlServer(connectionString)
|
||||
);
|
||||
|
||||
builder.Services.AddScoped(typeof(IStandManagerGenericRepository<>), typeof(StandManagerGenericRepository<>));
|
||||
builder.Services.AddScoped<IStandManagerUnitOfWork, StandManagerUnitOfWork>();
|
||||
builder.Services.AddScoped<IManagerService, ManagerService>();
|
||||
|
||||
builder.Services.AddScoped<LayoutState>();
|
||||
builder.Services.AddRadzenComponents();
|
||||
|
||||
builder.Services
|
||||
.AddAuthentication(options =>
|
||||
{
|
||||
options.DefaultAuthenticateScheme = "standmanager";
|
||||
options.DefaultSignInScheme = "standmanager";
|
||||
options.DefaultChallengeScheme = "standmanager";
|
||||
})
|
||||
.AddCookie("standmanager", options =>
|
||||
{
|
||||
options.LoginPath = "/management/login";
|
||||
options.AccessDeniedPath = "/management/access-denied";
|
||||
options.ExpireTimeSpan = TimeSpan.FromHours(8);
|
||||
options.SlidingExpiration = true;
|
||||
});
|
||||
|
||||
builder.Services.AddAuthorization();
|
||||
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
|
||||
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
|
||||
.AddCookie(options =>
|
||||
{
|
||||
options.Cookie.Name = "auth_token";
|
||||
options.Cookie.MaxAge = TimeSpan.FromMinutes(30);
|
||||
options.LoginPath = "/account/login";
|
||||
options.AccessDeniedPath = "/access-denied";
|
||||
});
|
||||
builder.Services.AddAuthorization();
|
||||
builder.Services.AddCascadingAuthenticationState();
|
||||
|
||||
builder.Services.AddDbContext<StandManagerDbContext>(options =>
|
||||
{
|
||||
options.UseSqlServer(connectionString);
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
@ -72,11 +90,34 @@ else
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseStaticFiles();
|
||||
|
||||
app.UseAntiforgery();
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapRazorComponents<App>()
|
||||
.AddInteractiveServerRenderMode();
|
||||
|
||||
app.Use(async (context, func) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await func();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
var logger = context.RequestServices.GetService<ILogger<Program>>();
|
||||
// Log dell'eccezione
|
||||
logger?.LogError(e, "Si è verificata un'eccezione durante l'elaborazione della richiesta.");
|
||||
|
||||
context.Response.Clear();
|
||||
|
||||
context.Response.StatusCode = e is BadHttpRequestException badHttpRequestException
|
||||
? badHttpRequestException.StatusCode
|
||||
: 500;
|
||||
}
|
||||
});
|
||||
|
||||
await app.RunAsync();
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
@ -8,6 +8,8 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.22" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.22" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.11">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"ConnectionStrings": {
|
||||
"ConnectionString": "Data Source=192.168.0.233\\SQL2019;Initial Catalog=DAC_Eventi;Persist Security Info=True;User ID=dac_user;Password=KZ4ZrUPzJV;TrustServerCertificate=True"
|
||||
"ConnectionString": "Data Source=192.168.0.233\\SQL2019;Initial Catalog=DAC_StandManager;Persist Security Info=True;User ID=dac_user;Password=KZ4ZrUPzJV;TrustServerCertificate=True"
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
|
||||
Reference in New Issue
Block a user