parent
9b7289569a
commit
29381c25fa
@ -0,0 +1,6 @@
|
|||||||
|
@page
|
||||||
|
@model BlazorCookieAuth.Server.Pages.LoginModel
|
||||||
|
@{
|
||||||
|
ViewData["Title"] = "Log in";
|
||||||
|
}
|
||||||
|
<h2>Login</h2>
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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 {
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue