diff --git a/TestApp02/App.razor b/TestApp02/App.razor index 843f201..d015311 100644 --- a/TestApp02/App.razor +++ b/TestApp02/App.razor @@ -1,6 +1,9 @@ - + + - + + + @**@ Not found @@ -8,4 +11,5 @@

Sorry, there's nothing at this address.

-
\ No newline at end of file +
+ diff --git a/TestApp02/Pages/Index.razor b/TestApp02/Pages/Index.razor index 7994d54..6eca05f 100644 --- a/TestApp02/Pages/Index.razor +++ b/TestApp02/Pages/Index.razor @@ -2,6 +2,43 @@ Index -Hello, world! +@*Hello, world! Welcome to your new app, powered by MudBlazor! You can find documentation and examples on our website here: www.mudblazor.com +*@ + + + Hello, world! + Hello, @context.User.Identity.Name! +

@authMessage

+
+ +

@authMessage

+ Hello, @context.User.Identity.Name! +@* Welcome to your new app, powered by MudBlazor! + You can find documentation and examples on our website here: www.mudblazor.com*@ +
+ +
+ + +@code { + private string authMessage = "The user is NOT authenticated."; + + [CascadingParameter] + private Task? authenticationState { get; set; } + + protected override async Task OnInitializedAsync() + { + if (authenticationState is not null) + { + var authState = await authenticationState; + var user = authState?.User; + + if (user?.Identity is not null && user.Identity.IsAuthenticated) + { + authMessage = $"{user.Identity.Name} is authenticated."; + } + } + } +} \ No newline at end of file diff --git a/TestApp02/Pages/Login.cshtml.cs b/TestApp02/Pages/Login.cshtml.cs index 3c2dd15..cc8f22e 100644 --- a/TestApp02/Pages/Login.cshtml.cs +++ b/TestApp02/Pages/Login.cshtml.cs @@ -30,49 +30,13 @@ namespace BlazorCookieAuth.Server.Pages if (ModelState.IsValid) { ClaimsPrincipal claimsPrincipal = _biudAuthService.AuthenticateUser(paramUsername, paramPassword); + + _biudAuthService.Users.Add(paramUsername,claimsPrincipal); await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal); + return LocalRedirect(returnUrl); } + return Page(); - //try - //{ - // // Clear the existing external cookie - // await HttpContext - // .SignOutAsync( - // CookieAuthenticationDefaults.AuthenticationScheme); - //} - //catch { } - - //// *** !!! This is where you would validate the user !!! *** - //// In this example we just log the user in - //// (Always log the user in for this demo) - - //var claims = new List - //{ - // new Claim(ClaimTypes.Name, paramUsername), - // new Claim(ClaimTypes.Role, "Administrator"), - //}; - - //var claimsIdentity = new ClaimsIdentity( - // claims, CookieAuthenticationDefaults.AuthenticationScheme); - - //var authProperties = new AuthenticationProperties - //{ - // IsPersistent = true, - // RedirectUri = this.Request.Host.Value - //}; - - //try - //{ - // await HttpContext.SignInAsync( - // CookieAuthenticationDefaults.AuthenticationScheme, - // new ClaimsPrincipal(claimsIdentity), - // authProperties); - //} - //catch (Exception ex) - //{ - // string error = ex.Message; - //} - return LocalRedirect(returnUrl); } } } \ No newline at end of file diff --git a/TestApp02/Pages/LoginMud.razor.cs b/TestApp02/Pages/LoginMud.razor.cs index 219a37a..7d03869 100644 --- a/TestApp02/Pages/LoginMud.razor.cs +++ b/TestApp02/Pages/LoginMud.razor.cs @@ -1,11 +1,6 @@ using Microsoft.AspNetCore.Components; using System.ComponentModel.DataAnnotations; -using Microsoft.AspNetCore.Authentication; -using Microsoft.AspNetCore.Authentication.Cookies; using TestApp02.Data; -using Microsoft.AspNetCore.Mvc.ModelBinding; -using System.Security.Claims; -using Microsoft.AspNetCore.Authentication.Cookies; using System.Web; namespace TestApp02.Pages @@ -29,15 +24,6 @@ namespace TestApp02.Pages protected async Task OnPostAsync() { - - //ClaimsPrincipal principal; - //principal = _BiudAuthService.AuthenticateUser(loginAccountForm.Username, loginAccountForm.Password); - - //if (principal == null) { - // //await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal); - //}; - - //var identity = principal.Identity as ClaimsIdentity; string _uName = encode(loginAccountForm.Username); string _uPass = encode(loginAccountForm.Password); diff --git a/TestApp02/Program.cs b/TestApp02/Program.cs index 767ddde..460ac5b 100644 --- a/TestApp02/Program.cs +++ b/TestApp02/Program.cs @@ -12,13 +12,12 @@ StaticWebAssetsLoader.UseStaticWebAssets(builder.Environment, builder.Configurat // Add services to the container. builder.Services.AddRazorPages(); builder.Services.AddServerSideBlazor(); +builder.Services.AddMudServices(); -builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) - .AddCookie(); +builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); -builder.Services.AddMudServices(); var app = builder.Build(); @@ -31,7 +30,8 @@ if (!app.Environment.IsDevelopment()) } app.UseHttpsRedirection(); - +app.UseAuthentication(); +app.UseAuthorization(); app.UseStaticFiles(); app.UseRouting();