Travail en cours page de login

This commit is contained in:
2026-01-18 16:35:14 +01:00
parent 29c33d015d
commit 614f64ddbb
4 changed files with 66 additions and 44 deletions

View File

@@ -5,75 +5,93 @@
@using Microsoft.AspNetCore.Authentication @using Microsoft.AspNetCore.Authentication
@using Microsoft.AspNetCore.Authentication.Cookies @using Microsoft.AspNetCore.Authentication.Cookies
@using Microsoft.EntityFrameworkCore @using Microsoft.EntityFrameworkCore
@inject AppDbContext DbContext @inject AuthenticationStateProvider AuthenticationStateProvider
@inject NavigationManager NavigationManager @inject NavigationManager NavigationManager
@inject IAuthService AuthService
<div class="row"> <div class="d-flex justify-content-center align-items-center">
<div class="col-lg-4 offset-lg-4 pt-4 pb-4 border"> <div class="col-md-4 p-5 shadow-sm border rounded-3">
<EditForm Model="Model" OnValidSubmit="Authenticate" FormName="LoginForm"> <h2 class="text-center mb-4 text-primary">Login Form</h2>
<DataAnnotationsValidator/> <EditForm Model="user" OnValidSubmit="Authenticate">
<div class="mb-3 text-center flex-column"> <DataAnnotationsValidator />
<img src="/images/login.png" style="max-height:5rem;"/> <div class="mb-3">
<h3>LOGIN</h3> <label for="email">User Name</label>
<InputText id="email" @bind-Value="userLogin.UserName" class="form-control border border-primary" />
<ValidationMessage For="@(() => userLogin.UserName)" />
</div> </div>
<div class="mb-3"> <div class="mb-3">
<label>User Name</label> <label for="password">Password</label>
<InputText @bind-Value="Model.UserName" class="form-control" placeholder="Enter User Name"/> <InputText id="password" @bind-Value="userLogin.Password" class="form-control border border-primary" type="password" />
<ValidationMessage For="() => Model.UserName"></ValidationMessage> <ValidationMessage For="@(() => userLogin.Password)" />
</div> </div>
<div class="mb-3"> <div class="d-grid">
<label>Password</label> <button type="submit" class="btn btn-primary">Login</button>
<InputText @bind-Value="Model.Password" class="form-control" placeholder="Enter Password"/>
<ValidationMessage For="() => Model.Password"></ValidationMessage>
</div> </div>
<div class="mb-3 text-center"> <div class="mt-3">
<span class="text-danger">@_errorMessage</span> <a href="#">Forgot password</a>
</div> </div>
<div class="mb-3 d-grid gap-2"> <div class="mt-3">
<button class="btn btn-primary" type="submit">Login</button> <p class="mb-0 text-center">You don't have an account? <a href="register" class="text-primary fw-bold">Register</a></p>
</div> </div>
</EditForm> </EditForm>
</div> </div>
</div> </div>
@code { @code {
[CascadingParameter] // [CascadingParameter]
public HttpContext? HttpContext { get; set; } // public HttpContext? HttpContext { get; set; }
[SupplyParameterFromForm] // [SupplyParameterFromForm]
public LoginViewModel Model { get; set; } = new(); // public LoginViewModel Model { get; set; } = new();
private UserLogin userLogin = new();
private string? _errorMessage; private string? _errorMessage;
private async Task Authenticate() 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"; errorMessage = string.Empty;
return; // if (rememberMe)
// await LocalStorageService.SetItemAsync("authToken", result.Data);
// else
// await SessionStorageService.SetItemAsync("authToken", result.Data);
await AuthenticationStateProvider.GetAuthenticationStateAsync();
//NavigationManager.NavigateTo(returnUrl);
NavigationManager.NavigateTo("/");
} }
else
var userAccount = DbContext.UserAccounts.FirstOrDefault(x => x.UserName == Model.UserName);
if (userAccount is null || userAccount.Password != Model.Password)
{ {
_errorMessage = "Invalid User Name or Password"; errorMessage = result.Message;
return;
} }
// if(string.IsNullOrWhiteSpace(Model.UserName) || string.IsNullOrWhiteSpace(Model.Password))
// {
// _errorMessage = "Invalid User Name or Password";
// return;
// }
var claims = new List<Claim> // var userAccount = DbContext.UserAccounts.FirstOrDefault(x => x.UserName == Model.UserName);
{ // if (userAccount is null || userAccount.Password != Model.Password)
new Claim(ClaimTypes.Name, Model.UserName) // {
}; // _errorMessage = "Invalid User Name or Password";
// return;
// }
/* Add Policies */ // var claims = new List<Claim>
var userAccountPolicies = await DbContext.UserAccountPolicies.Where(x => x.UserAccountId == userAccount.Id && x.IsEnabled).ToListAsync(); // {
claims.AddRange(userAccountPolicies.Select(userAccountPolicy => new Claim(userAccountPolicy.UserPolicy, "true"))); // new Claim(ClaimTypes.Name, Model.UserName)
// };
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); // /* Add Policies */
var principal = new ClaimsPrincipal(identity); // var userAccountPolicies = await DbContext.UserAccountPolicies.Where(x => x.UserAccountId == userAccount.Id && x.IsEnabled).ToListAsync();
await HttpContext?.SignInAsync(principal)!; // claims.AddRange(userAccountPolicies.Select(userAccountPolicy => new Claim(userAccountPolicy.UserPolicy, "true")));
NavigationManager.NavigateTo("/");
// var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
// var principal = new ClaimsPrincipal(identity);
// await HttpContext?.SignInAsync(principal)!;
// NavigationManager.NavigateTo("/");
} }
} }

View File

@@ -10,3 +10,4 @@
@using BlazorPolicyAuth.Components @using BlazorPolicyAuth.Components
@using BlazorPolicyAuth.Components.Layout @using BlazorPolicyAuth.Components.Layout
@using Microsoft.AspNetCore.Components.Authorization @using Microsoft.AspNetCore.Components.Authorization
@using BlazorPolicyAuth.Services.AuthService

View File

@@ -1,6 +1,7 @@
using BlazorPolicyAuth; using BlazorPolicyAuth;
using BlazorPolicyAuth.Components; using BlazorPolicyAuth.Components;
using BlazorPolicyAuth.Data; using BlazorPolicyAuth.Data;
using BlazorPolicyAuth.Services.AuthService;
using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@@ -31,6 +32,8 @@ builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationSc
builder.Services.AddCascadingAuthenticationState(); builder.Services.AddCascadingAuthenticationState();
builder.Services.AddScoped<IAuthService, AuthService>();
var app = builder.Build(); var app = builder.Build();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.

View File

@@ -10,7 +10,7 @@ using System.Security.Claims;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
namespace BlazorAuth.Server.Services.AuthService; namespace BlazorPolicyAuth.Services.AuthService;
public class AuthService : IAuthService public class AuthService : IAuthService
{ {