Test Autentification 20230216
This commit is contained in:
30
TestApp02/Data/BiudAuthService.cs
Normal file
30
TestApp02/Data/BiudAuthService.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace TestApp02.Data
|
||||
{
|
||||
public class BiudAuthService
|
||||
{
|
||||
public BiudAuthService() {
|
||||
Users = new Dictionary<string, ClaimsPrincipal> ();
|
||||
}
|
||||
public Dictionary<string, ClaimsPrincipal> Users { get; set; }
|
||||
|
||||
public ClaimsPrincipal AuthenticateUser(string p_LoginUser, string p_PasswordUser)
|
||||
{
|
||||
|
||||
//ClaimsIdentity _ClaimsIdentity = new ClaimsIdentity();
|
||||
|
||||
List<Claim> _Claims = new List<Claim>{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
6
TestApp02/Pages/Login.cshtml
Normal file
6
TestApp02/Pages/Login.cshtml
Normal file
@@ -0,0 +1,6 @@
|
||||
@page
|
||||
@model BlazorCookieAuth.Server.Pages.LoginModel
|
||||
@{
|
||||
ViewData["Title"] = "Log in";
|
||||
}
|
||||
<h2>Login</h2>
|
||||
78
TestApp02/Pages/Login.cshtml.cs
Normal file
78
TestApp02/Pages/Login.cshtml.cs
Normal file
@@ -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<IActionResult> 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<Claim>
|
||||
//{
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
31
TestApp02/Pages/LoginMud.razor
Normal file
31
TestApp02/Pages/LoginMud.razor
Normal file
@@ -0,0 +1,31 @@
|
||||
@page "/loginr"
|
||||
@inherits LoginModel;
|
||||
@using System.ComponentModel.DataAnnotations
|
||||
@using System.Web;
|
||||
|
||||
<EditForm Model="@loginAccountForm" OnValidSubmit="OnPostAsync">
|
||||
<DataAnnotationsValidator />
|
||||
<MudGrid>
|
||||
<MudItem xs="12" sm="7">
|
||||
<MudCard>
|
||||
@*<DataAnnotationsValidator/>*@
|
||||
<ValidationSummary/>
|
||||
|
||||
<MudCardContent>
|
||||
<MudTextField Label="Логин" HelperText="Введите логин"
|
||||
@bind-Value="loginAccountForm.Username" For="@(() => loginAccountForm.Username)" />
|
||||
<MudTextField Label="Пароль" HelperText="Введите пароль" Class="mt-3"
|
||||
@bind-Value="loginAccountForm.Password" For="@(() => loginAccountForm.Password)" InputType="InputType.Password" />
|
||||
</MudCardContent>
|
||||
<MudCardActions>
|
||||
<MudButton ButtonType="ButtonType.Submit" Variant="Variant.Filled" Color="Color.Primary" Class="ml-auto">Зарегистрироваться</MudButton>
|
||||
</MudCardActions>
|
||||
</MudCard>
|
||||
</MudItem>
|
||||
|
||||
</MudGrid>
|
||||
</EditForm>
|
||||
|
||||
@code {
|
||||
|
||||
}
|
||||
64
TestApp02/Pages/LoginMud.razor.cs
Normal file
64
TestApp02/Pages/LoginMud.razor.cs
Normal file
@@ -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; }
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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<BiudAuthService>();
|
||||
|
||||
builder.Services.AddSingleton<WeatherForecastService>();
|
||||
builder.Services.AddMudServices();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user