From 677fc4a1a40e0ee421c40a6934575b70ed622102 Mon Sep 17 00:00:00 2001 From: 084MochalinPA Date: Thu, 16 Feb 2023 21:57:44 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B0=D1=83=D1=82=D0=B5=D0=BD=D1=82=D0=B8=D1=84?= =?UTF-8?q?=D0=B8=D0=BA=D0=B0=D1=86=D1=8F=20=D1=84=D0=B5=D0=B9=D0=BA=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D1=8F=20=D0=A0=D0=90=D0=91=D0=9E=D0=A2=D0=90=D0=95?= =?UTF-8?q?=D0=A2!!!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TestApp02/App.razor | 10 ++++--- TestApp02/Pages/Index.razor | 39 ++++++++++++++++++++++++++- TestApp02/Pages/Login.cshtml.cs | 44 +++---------------------------- TestApp02/Pages/LoginMud.razor.cs | 14 ---------- TestApp02/Program.cs | 8 +++--- 5 files changed, 53 insertions(+), 62 deletions(-) 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();