From 29381c25fa21df5a4a1ea765e33ef24b9559a21e Mon Sep 17 00:00:00 2001 From: 084MochalinPA Date: Thu, 16 Feb 2023 20:40:06 +0300 Subject: [PATCH] Test Autentification 20230216 --- TestApp02/Data/BiudAuthService.cs | 30 ++++++++++++ TestApp02/Pages/Login.cshtml | 6 +++ TestApp02/Pages/Login.cshtml.cs | 78 +++++++++++++++++++++++++++++++ TestApp02/Pages/LoginMud.razor | 31 ++++++++++++ TestApp02/Pages/LoginMud.razor.cs | 64 +++++++++++++++++++++++++ TestApp02/Program.cs | 6 +++ 6 files changed, 215 insertions(+) create mode 100644 TestApp02/Data/BiudAuthService.cs create mode 100644 TestApp02/Pages/Login.cshtml create mode 100644 TestApp02/Pages/Login.cshtml.cs create mode 100644 TestApp02/Pages/LoginMud.razor create mode 100644 TestApp02/Pages/LoginMud.razor.cs diff --git a/TestApp02/Data/BiudAuthService.cs b/TestApp02/Data/BiudAuthService.cs new file mode 100644 index 0000000..7c5eb82 --- /dev/null +++ b/TestApp02/Data/BiudAuthService.cs @@ -0,0 +1,30 @@ +using Microsoft.AspNetCore.Authentication.Cookies; +using System.Security.Claims; + +namespace TestApp02.Data +{ + public class BiudAuthService + { + public BiudAuthService() { + Users = new Dictionary (); + } + public Dictionary Users { get; set; } + + public ClaimsPrincipal AuthenticateUser(string p_LoginUser, string p_PasswordUser) + { + + //ClaimsIdentity _ClaimsIdentity = new ClaimsIdentity(); + + List _Claims = new List{ + new Claim(ClaimTypes.Name, p_LoginUser), + new Claim("Surname", "Surname"), + new Claim ("Firstname", "Firstname"), + new Claim ("Middlename", "Middlename"), + new Claim ("Raion", "raion") + }; + ClaimsIdentity _ClaimsIdentity = new ClaimsIdentity(_Claims, CookieAuthenticationDefaults.AuthenticationScheme); + ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(_ClaimsIdentity); + return claimsPrincipal; + } + } +} diff --git a/TestApp02/Pages/Login.cshtml b/TestApp02/Pages/Login.cshtml new file mode 100644 index 0000000..d4632da --- /dev/null +++ b/TestApp02/Pages/Login.cshtml @@ -0,0 +1,6 @@ +@page +@model BlazorCookieAuth.Server.Pages.LoginModel +@{ + ViewData["Title"] = "Log in"; +} +

Login

\ No newline at end of file diff --git a/TestApp02/Pages/Login.cshtml.cs b/TestApp02/Pages/Login.cshtml.cs new file mode 100644 index 0000000..3c2dd15 --- /dev/null +++ b/TestApp02/Pages/Login.cshtml.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Security.Claims; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.RazorPages; +using TestApp02.Data; + +namespace BlazorCookieAuth.Server.Pages +{ + [AllowAnonymous] + public class LoginModel : PageModel + { + public string ReturnUrl { get; set; } + + private readonly BiudAuthService _biudAuthService; + + public LoginModel(BiudAuthService pbiudAuthService) + { + _biudAuthService= pbiudAuthService; + } + + public async Task OnGetAsync(string paramUsername, string paramPassword) + { + string returnUrl = Url.Content("~/"); + + if (ModelState.IsValid) + { + ClaimsPrincipal claimsPrincipal = _biudAuthService.AuthenticateUser(paramUsername, paramPassword); + await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal); + } + + //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 b/TestApp02/Pages/LoginMud.razor new file mode 100644 index 0000000..faf1297 --- /dev/null +++ b/TestApp02/Pages/LoginMud.razor @@ -0,0 +1,31 @@ +@page "/loginr" +@inherits LoginModel; +@using System.ComponentModel.DataAnnotations +@using System.Web; + + + + + + + @**@ + + + + + + + + Зарегистрироваться + + + + + + + +@code { + +} \ No newline at end of file diff --git a/TestApp02/Pages/LoginMud.razor.cs b/TestApp02/Pages/LoginMud.razor.cs new file mode 100644 index 0000000..219a37a --- /dev/null +++ b/TestApp02/Pages/LoginMud.razor.cs @@ -0,0 +1,64 @@ +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 +{ + public class LoginModel: ComponentBase + { + [Inject] NavigationManager _navigationManager { get; set; } + + //private BiudAuthService _BiudAuthService; + [Inject] BiudAuthService _BiudAuthService { get; set; } + + //public LoginModel(BiudAuthService pBiudAuthService) + public LoginModel() + { + + loginAccountForm= new LoginAccountForm(); + } + + + public LoginAccountForm loginAccountForm { get; set;} + + 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); + + _navigationManager.NavigateTo("/login?paramUsername="+ _uName + "¶mPassword=" + _uPass, true); + } + + private string encode(string param) + { + return HttpUtility.UrlEncode(param); + } + } + + public class LoginAccountForm + { + [Required (ErrorMessage = "Необходим логин")] + [StringLength(30, ErrorMessage = "Допустимая длина логина от 3 до 30 символов ", MinimumLength = 3)] + public string Username { get; set; } + + [Required(ErrorMessage = "Необходим пароль")] + [StringLength(30, ErrorMessage = "Допустимая длина пароля от 3 до 30 символов ", MinimumLength = 3)] + public string Password { get; set; } + + } +} diff --git a/TestApp02/Program.cs b/TestApp02/Program.cs index d3cbf13..767ddde 100644 --- a/TestApp02/Program.cs +++ b/TestApp02/Program.cs @@ -1,3 +1,4 @@ +using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Hosting.StaticWebAssets; @@ -11,6 +12,11 @@ StaticWebAssetsLoader.UseStaticWebAssets(builder.Environment, builder.Configurat // Add services to the container. builder.Services.AddRazorPages(); builder.Services.AddServerSideBlazor(); + +builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) + .AddCookie(); +builder.Services.AddSingleton(); + builder.Services.AddSingleton(); builder.Services.AddMudServices();