From 614f64ddbbe2974c49f296d33ff62c4b38f3d942 Mon Sep 17 00:00:00 2001 From: Barbara Post Date: Sun, 18 Jan 2026 16:35:14 +0100 Subject: [PATCH] Travail en cours page de login --- .../Components/Pages/Account/Login.razor | 104 ++++++++++-------- BlazorPolicyAuth/Components/_Imports.razor | 1 + BlazorPolicyAuth/Program.cs | 3 + .../Services/AuthService/AuthService.cs | 2 +- 4 files changed, 66 insertions(+), 44 deletions(-) diff --git a/BlazorPolicyAuth/Components/Pages/Account/Login.razor b/BlazorPolicyAuth/Components/Pages/Account/Login.razor index 18750b7..584f5c0 100644 --- a/BlazorPolicyAuth/Components/Pages/Account/Login.razor +++ b/BlazorPolicyAuth/Components/Pages/Account/Login.razor @@ -5,75 +5,93 @@ @using Microsoft.AspNetCore.Authentication @using Microsoft.AspNetCore.Authentication.Cookies @using Microsoft.EntityFrameworkCore -@inject AppDbContext DbContext +@inject AuthenticationStateProvider AuthenticationStateProvider @inject NavigationManager NavigationManager +@inject IAuthService AuthService -
-
- - -
- -

LOGIN

+
+
+

Login Form

+ + +
+ + +
- - - + + +
-
- - - +
+
-
- @_errorMessage + -
- +
+

You don't have an account? Register

- @code { - [CascadingParameter] - public HttpContext? HttpContext { get; set; } + // [CascadingParameter] + // public HttpContext? HttpContext { get; set; } - [SupplyParameterFromForm] - public LoginViewModel Model { get; set; } = new(); + // [SupplyParameterFromForm] + // public LoginViewModel Model { get; set; } = new(); + private UserLogin userLogin = new(); private string? _errorMessage; private async Task Authenticate() { - if(string.IsNullOrWhiteSpace(Model.UserName) || string.IsNullOrWhiteSpace(Model.Password)) + var result = await AuthService.Login(user); + if (result.Success) { - _errorMessage = "Invalid User Name or Password"; - return; + errorMessage = string.Empty; + // if (rememberMe) + // await LocalStorageService.SetItemAsync("authToken", result.Data); + // else + // await SessionStorageService.SetItemAsync("authToken", result.Data); + + await AuthenticationStateProvider.GetAuthenticationStateAsync(); + //NavigationManager.NavigateTo(returnUrl); + NavigationManager.NavigateTo("/"); } - - var userAccount = DbContext.UserAccounts.FirstOrDefault(x => x.UserName == Model.UserName); - if (userAccount is null || userAccount.Password != Model.Password) + else { - _errorMessage = "Invalid User Name or Password"; - return; + errorMessage = result.Message; } + // if(string.IsNullOrWhiteSpace(Model.UserName) || string.IsNullOrWhiteSpace(Model.Password)) + // { + // _errorMessage = "Invalid User Name or Password"; + // return; + // } - var claims = new List - { - new Claim(ClaimTypes.Name, Model.UserName) - }; + // var userAccount = DbContext.UserAccounts.FirstOrDefault(x => x.UserName == Model.UserName); + // if (userAccount is null || userAccount.Password != Model.Password) + // { + // _errorMessage = "Invalid User Name or Password"; + // return; + // } - /* Add Policies */ - var userAccountPolicies = await DbContext.UserAccountPolicies.Where(x => x.UserAccountId == userAccount.Id && x.IsEnabled).ToListAsync(); - claims.AddRange(userAccountPolicies.Select(userAccountPolicy => new Claim(userAccountPolicy.UserPolicy, "true"))); + // var claims = new List + // { + // new Claim(ClaimTypes.Name, Model.UserName) + // }; - var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); - var principal = new ClaimsPrincipal(identity); - await HttpContext?.SignInAsync(principal)!; - NavigationManager.NavigateTo("/"); + // /* Add Policies */ + // var userAccountPolicies = await DbContext.UserAccountPolicies.Where(x => x.UserAccountId == userAccount.Id && x.IsEnabled).ToListAsync(); + // claims.AddRange(userAccountPolicies.Select(userAccountPolicy => new Claim(userAccountPolicy.UserPolicy, "true"))); + + // var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); + // var principal = new ClaimsPrincipal(identity); + // await HttpContext?.SignInAsync(principal)!; + // NavigationManager.NavigateTo("/"); } } \ No newline at end of file diff --git a/BlazorPolicyAuth/Components/_Imports.razor b/BlazorPolicyAuth/Components/_Imports.razor index 966a7ad..c789c3c 100644 --- a/BlazorPolicyAuth/Components/_Imports.razor +++ b/BlazorPolicyAuth/Components/_Imports.razor @@ -10,3 +10,4 @@ @using BlazorPolicyAuth.Components @using BlazorPolicyAuth.Components.Layout @using Microsoft.AspNetCore.Components.Authorization +@using BlazorPolicyAuth.Services.AuthService \ No newline at end of file diff --git a/BlazorPolicyAuth/Program.cs b/BlazorPolicyAuth/Program.cs index c3354e4..bec6c80 100644 --- a/BlazorPolicyAuth/Program.cs +++ b/BlazorPolicyAuth/Program.cs @@ -1,6 +1,7 @@ using BlazorPolicyAuth; using BlazorPolicyAuth.Components; using BlazorPolicyAuth.Data; +using BlazorPolicyAuth.Services.AuthService; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.EntityFrameworkCore; @@ -31,6 +32,8 @@ builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationSc builder.Services.AddCascadingAuthenticationState(); +builder.Services.AddScoped(); + var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/BlazorPolicyAuth/Services/AuthService/AuthService.cs b/BlazorPolicyAuth/Services/AuthService/AuthService.cs index 6237c9e..e9a015e 100644 --- a/BlazorPolicyAuth/Services/AuthService/AuthService.cs +++ b/BlazorPolicyAuth/Services/AuthService/AuthService.cs @@ -10,7 +10,7 @@ using System.Security.Claims; using System.Security.Cryptography; using System.Text; -namespace BlazorAuth.Server.Services.AuthService; +namespace BlazorPolicyAuth.Services.AuthService; public class AuthService : IAuthService {