Travail sur login
This commit is contained in:
@@ -8,10 +8,12 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.1">
|
||||
<PackageReference Include="Blazored.LocalStorage" Version="4.5.0" />
|
||||
<PackageReference Include="Blazored.SessionStorage" Version="2.4.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.2" />
|
||||
<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>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
|
||||
@@ -1,24 +1,36 @@
|
||||
@page "/login"
|
||||
@using System.Web
|
||||
@using System.Collections.Specialized
|
||||
@using Blazored.LocalStorage
|
||||
@using Blazored.SessionStorage
|
||||
@using BlazorPolicyAuth.App.Services.AuthService
|
||||
@using BlazorPolicyAuth.Models.ViewModels
|
||||
@inject AuthenticationStateProvider AuthenticationStateProvider
|
||||
@inject NavigationManager NavigationManager
|
||||
@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="col-md-4 p-5 shadow-sm border rounded-3">
|
||||
<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 />
|
||||
<div class="mb-3">
|
||||
<label for="email">User Name</label>
|
||||
<InputText id="email" @bind-Value="userLogin.Email" class="form-control border border-primary" />
|
||||
<ValidationMessage For="@(() => userLogin.Email)" />
|
||||
<label for="email">Email</label>
|
||||
<InputText id="email" @bind-Value="user.Email" class="form-control border border-primary" />
|
||||
<ValidationMessage For="@(() => user.Email)" />
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="password">Password</label>
|
||||
<InputText id="password" @bind-Value="userLogin.Password" class="form-control border border-primary" type="password" />
|
||||
<ValidationMessage For="@(() => userLogin.Password)" />
|
||||
<InputText id="password" @bind-Value="user.Password" class="form-control border border-primary" type="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 class="d-grid">
|
||||
<button type="submit" class="btn btn-primary">Login</button>
|
||||
@@ -32,64 +44,44 @@
|
||||
</EditForm>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="text-danger">
|
||||
<span>@errorMessage</span>
|
||||
</div>
|
||||
|
||||
@code {
|
||||
// [CascadingParameter]
|
||||
// public HttpContext? HttpContext { get; set; }
|
||||
private UserLogin user = new();
|
||||
private string errorMessage = string.Empty;
|
||||
private string returnUrl = string.Empty;
|
||||
private bool rememberMe;
|
||||
|
||||
// [SupplyParameterFromForm]
|
||||
// public LoginViewModel Model { get; set; } = new();
|
||||
|
||||
private UserLogin userLogin = new();
|
||||
private string? _errorMessage;
|
||||
|
||||
private async Task Authenticate()
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
Console.WriteLine("***");
|
||||
Console.WriteLine(userLogin.Email);
|
||||
var result = await AuthService.Login(userLogin);
|
||||
var uri = NavigationManager.ToAbsoluteUri(NavigationManager.Uri);
|
||||
NameValueCollection queryStringCall = HttpUtility.ParseQueryString(uri.Query);
|
||||
|
||||
if (queryStringCall.AllKeys.Contains("returnUrl"))
|
||||
{
|
||||
returnUrl = queryStringCall["returnUrl"];
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleLogin()
|
||||
{
|
||||
var result = await AuthService.Login(user);
|
||||
if (result.Success)
|
||||
{
|
||||
_errorMessage = string.Empty;
|
||||
// if (rememberMe)
|
||||
// await LocalStorageService.SetItemAsync("authToken", result.Data);
|
||||
// else
|
||||
// await SessionStorageService.SetItemAsync("authToken", result.Data);
|
||||
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("/");
|
||||
NavigationManager.NavigateTo(returnUrl);
|
||||
}
|
||||
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("/");
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using Blazored.LocalStorage;
|
||||
using Blazored.SessionStorage;
|
||||
using BlazorPolicyAuth;
|
||||
using BlazorPolicyAuth.Components;
|
||||
using BlazorPolicyAuth.Data;
|
||||
@@ -41,6 +43,9 @@ builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationSc
|
||||
builder.Services.AddCascadingAuthenticationState();
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
|
||||
builder.Services.AddBlazoredLocalStorage();
|
||||
builder.Services.AddBlazoredSessionStorage();
|
||||
|
||||
// Blazor client services
|
||||
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) =>
|
||||
await authService.Register(request));
|
||||
|
||||
app.MapPost("/api/auth/login", async (UserLogin request, IAuthService authService) =>
|
||||
await authService.Login(request.Email, request.Password));
|
||||
|
||||
app.Run();
|
||||
|
||||
Reference in New Issue
Block a user