diff --git a/BlazorPolicyAuth/BlazorPolicyAuth.csproj b/BlazorPolicyAuth/BlazorPolicyAuth.csproj index 1b2989d..058a51e 100644 --- a/BlazorPolicyAuth/BlazorPolicyAuth.csproj +++ b/BlazorPolicyAuth/BlazorPolicyAuth.csproj @@ -8,10 +8,12 @@ - - - - + + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/BlazorPolicyAuth/Components/Pages/Account/Login.razor b/BlazorPolicyAuth/Components/Pages/Account/Login.razor index e36f83f..30e0fab 100644 --- a/BlazorPolicyAuth/Components/Pages/Account/Login.razor +++ b/BlazorPolicyAuth/Components/Pages/Account/Login.razor @@ -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 + +Login

Login Form

- +
- - - + + +
- - + + +
+
+ +
@@ -32,64 +44,44 @@
- +
+ @errorMessage +
@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 - // { - // 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("/"); } } \ No newline at end of file diff --git a/BlazorPolicyAuth/Program.cs b/BlazorPolicyAuth/Program.cs index 34ef402..aa9f400 100644 --- a/BlazorPolicyAuth/Program.cs +++ b/BlazorPolicyAuth/Program.cs @@ -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(); @@ -73,4 +78,7 @@ app.MapRazorComponents() 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();