You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.5 KiB

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);
}
}
}