Travail sur login

This commit is contained in:
2026-02-01 16:25:30 +01:00
parent 846bb29914
commit e07f3f9541
3 changed files with 61 additions and 59 deletions

View File

@@ -8,10 +8,12 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.1" /> <PackageReference Include="Blazored.LocalStorage" Version="4.5.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.1" /> <PackageReference Include="Blazored.SessionStorage" Version="2.4.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.1" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.1"> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.2">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>

View File

@@ -1,24 +1,36 @@
@page "/login" @page "/login"
@using System.Web
@using System.Collections.Specialized
@using Blazored.LocalStorage
@using Blazored.SessionStorage
@using BlazorPolicyAuth.App.Services.AuthService @using BlazorPolicyAuth.App.Services.AuthService
@using BlazorPolicyAuth.Models.ViewModels @using BlazorPolicyAuth.Models.ViewModels
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject NavigationManager NavigationManager
@inject IAuthService AuthService @inject IAuthService AuthService
@inject ILocalStorageService LocalStorageService
@inject ISessionStorageService SessionStorageService
@inject NavigationManager NavigationManager
@inject AuthenticationStateProvider AuthenticationStateProvider
<PageTitle>Login</PageTitle>
<div class="d-flex justify-content-center align-items-center"> <div class="d-flex justify-content-center align-items-center">
<div class="col-md-4 p-5 shadow-sm border rounded-3"> <div class="col-md-4 p-5 shadow-sm border rounded-3">
<h2 class="text-center mb-4 text-primary">Login Form</h2> <h2 class="text-center mb-4 text-primary">Login Form</h2>
<EditForm Model="userLogin" OnValidSubmit="Authenticate" FormName="LoginForm"> <EditForm Model="user" OnValidSubmit="HandleLogin" FormName="loginForm">
<DataAnnotationsValidator /> <DataAnnotationsValidator />
<div class="mb-3"> <div class="mb-3">
<label for="email">User Name</label> <label for="email">Email</label>
<InputText id="email" @bind-Value="userLogin.Email" class="form-control border border-primary" /> <InputText id="email" @bind-Value="user.Email" class="form-control border border-primary" />
<ValidationMessage For="@(() => userLogin.Email)" /> <ValidationMessage For="@(() => user.Email)" />
</div> </div>
<div class="mb-3"> <div class="mb-3">
<label for="password">Password</label> <label for="password">Password</label>
<InputText id="password" @bind-Value="userLogin.Password" class="form-control border border-primary" type="password" /> <InputText id="password" @bind-Value="user.Password" class="form-control border border-primary" type="password" />
<ValidationMessage For="@(() => userLogin.Password)" /> <ValidationMessage For="@(() => user.Password)" />
</div>
<div class="mb-3">
<label for="rememberme">Remember me</label>
<InputCheckbox id="rememberme" @bind-Value="rememberMe" class="form-check-input" />
</div> </div>
<div class="d-grid"> <div class="d-grid">
<button type="submit" class="btn btn-primary">Login</button> <button type="submit" class="btn btn-primary">Login</button>
@@ -32,64 +44,44 @@
</EditForm> </EditForm>
</div> </div>
</div> </div>
<div class="text-danger">
<span>@errorMessage</span>
</div>
@code { @code {
// [CascadingParameter] private UserLogin user = new();
// public HttpContext? HttpContext { get; set; } private string errorMessage = string.Empty;
private string returnUrl = string.Empty;
private bool rememberMe;
// [SupplyParameterFromForm] protected override void OnInitialized()
// public LoginViewModel Model { get; set; } = new();
private UserLogin userLogin = new();
private string? _errorMessage;
private async Task Authenticate()
{ {
Console.WriteLine("***"); var uri = NavigationManager.ToAbsoluteUri(NavigationManager.Uri);
Console.WriteLine(userLogin.Email); NameValueCollection queryStringCall = HttpUtility.ParseQueryString(uri.Query);
var result = await AuthService.Login(userLogin);
if (queryStringCall.AllKeys.Contains("returnUrl"))
{
returnUrl = queryStringCall["returnUrl"];
}
}
private async Task HandleLogin()
{
var result = await AuthService.Login(user);
if (result.Success) if (result.Success)
{ {
_errorMessage = string.Empty; errorMessage = string.Empty;
// if (rememberMe) if (rememberMe)
// await LocalStorageService.SetItemAsync("authToken", result.Data); await LocalStorageService.SetItemAsync("authToken", result.Data);
// else else
// await SessionStorageService.SetItemAsync("authToken", result.Data); await SessionStorageService.SetItemAsync("authToken", result.Data);
await AuthenticationStateProvider.GetAuthenticationStateAsync(); await AuthenticationStateProvider.GetAuthenticationStateAsync();
//NavigationManager.NavigateTo(returnUrl); NavigationManager.NavigateTo(returnUrl);
NavigationManager.NavigateTo("/");
} }
else else
{ {
_errorMessage = result.Message; 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

@@ -1,5 +1,7 @@
using System; using System;
using System.Net.Http; using System.Net.Http;
using Blazored.LocalStorage;
using Blazored.SessionStorage;
using BlazorPolicyAuth; using BlazorPolicyAuth;
using BlazorPolicyAuth.Components; using BlazorPolicyAuth.Components;
using BlazorPolicyAuth.Data; using BlazorPolicyAuth.Data;
@@ -41,6 +43,9 @@ builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationSc
builder.Services.AddCascadingAuthenticationState(); builder.Services.AddCascadingAuthenticationState();
builder.Services.AddHttpContextAccessor(); builder.Services.AddHttpContextAccessor();
builder.Services.AddBlazoredLocalStorage();
builder.Services.AddBlazoredSessionStorage();
// Blazor client services // Blazor client services
builder.Services.AddScoped<ClientServices.AuthService.IAuthService, ClientServices.AuthService.AuthService>(); builder.Services.AddScoped<ClientServices.AuthService.IAuthService, ClientServices.AuthService.AuthService>();
@@ -73,4 +78,7 @@ app.MapRazorComponents<App>()
app.MapPost("/api/auth/register", async (UserRegister request, IAuthService authService) => app.MapPost("/api/auth/register", async (UserRegister request, IAuthService authService) =>
await authService.Register(request)); await authService.Register(request));
app.MapPost("/api/auth/login", async (UserLogin request, IAuthService authService) =>
await authService.Login(request.Email, request.Password));
app.Run(); app.Run();