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.Cookies
@using Microsoft.EntityFrameworkCore
@inject AppDbContext DbContext
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject NavigationManager NavigationManager
@inject IAuthService AuthService
<div class="row">
<div class="col-lg-4 offset-lg-4 pt-4 pb-4 border">
<EditForm Model="Model" OnValidSubmit="Authenticate" FormName="LoginForm">
<div class="d-flex justify-content-center align-items-center">
<div class="col-md-4 p-5 shadow-sm border rounded-3">
<h2 class="text-center mb-4 text-primary">Login Form</h2>
<EditForm Model="user" OnValidSubmit="Authenticate">
<DataAnnotationsValidator />
<div class="mb-3 text-center flex-column">
<img src="/images/login.png" style="max-height:5rem;"/>
<h3>LOGIN</h3>
<div class="mb-3">
<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 class="mb-3">
<label>User Name</label>
<InputText @bind-Value="Model.UserName" class="form-control" placeholder="Enter User Name"/>
<ValidationMessage For="() => Model.UserName"></ValidationMessage>
<label for="password">Password</label>
<InputText id="password" @bind-Value="userLogin.Password" class="form-control border border-primary" type="password" />
<ValidationMessage For="@(() => userLogin.Password)" />
</div>
<div class="mb-3">
<label>Password</label>
<InputText @bind-Value="Model.Password" class="form-control" placeholder="Enter Password"/>
<ValidationMessage For="() => Model.Password"></ValidationMessage>
<div class="d-grid">
<button type="submit" class="btn btn-primary">Login</button>
</div>
<div class="mb-3 text-center">
<span class="text-danger">@_errorMessage</span>
<div class="mt-3">
<a href="#">Forgot password</a>
</div>
<div class="mb-3 d-grid gap-2">
<button class="btn btn-primary" type="submit">Login</button>
<div class="mt-3">
<p class="mb-0 text-center">You don't have an account? <a href="register" class="text-primary fw-bold">Register</a></p>
</div>
</EditForm>
</div>
</div>
@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);
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;
}
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, Model.UserName)
};
/* 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)!;
await AuthenticationStateProvider.GetAuthenticationStateAsync();
//NavigationManager.NavigateTo(returnUrl);
NavigationManager.NavigateTo("/");
}
else
{
errorMessage = result.Message;
}
// if(string.IsNullOrWhiteSpace(Model.UserName) || string.IsNullOrWhiteSpace(Model.Password))
// {
// _errorMessage = "Invalid User Name or Password";
// return;
// }
// 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;
// }
// var claims = new List<Claim>
// {
// new Claim(ClaimTypes.Name, Model.UserName)
// };
// /* 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("/");
}
}

View File

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

View File

@@ -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<IAuthService, AuthService>();
var app = builder.Build();
// Configure the HTTP request pipeline.

View File

@@ -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
{