Files
Print_server/ConfigurationPannel/Pages/Login.cshtml.cs
2026-01-13 09:06:47 +01:00

54 lines
1.4 KiB
C#

using System.Security.Claims;
using ConfigurationPannel.Services;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace ConfigurationPannel.Pages;
public class LoginModel : PageModel
{
private readonly UserService _userService;
public LoginModel(UserService userService)
{
_userService = userService;
}
[BindProperty]
public string Username { get; set; } = string.Empty;
[BindProperty]
public string Password { get; set; } = string.Empty;
public string ErrorMessage { get; set; } = string.Empty;
public void OnGet()
{
}
public async Task<IActionResult> OnPostAsync()
{
if (await _userService.ValidateCredentialsAsync(Username, Password))
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, Username)
};
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var principal = new ClaimsPrincipal(identity);
await HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
principal);
return RedirectToPage("/Configure");
}
ErrorMessage = "Invalid username or password";
return Page();
}
}