@page "/login"
@using System.ComponentModel.DataAnnotations
@using Microsoft.AspNetCore.Authentication
@using Microsoft.AspNetCore.Authentication.Cookies
@layout TaxBaik.Admin.Components.Layout.BlankLayout
@attribute [AllowAnonymous]
로그인
관리자 로그인
@if (!string.IsNullOrEmpty(errorMessage))
{
@errorMessage
}
로그인
@using Microsoft.AspNetCore.Authentication
@using System.Security.Claims
@using TaxBaik.Domain.Interfaces
@code {
private MudForm form;
private bool isFormValid = false;
private string errorMessage = "";
private LoginModel model = new();
private bool isLoading = false;
[Inject]
private IAdminUserRepository AdminUserRepository { get; set; }
[Inject]
private NavigationManager NavigationManager { get; set; }
[Inject]
private HttpContextAccessor HttpContextAccessor { get; set; }
private async Task HandleLogin()
{
if (!isFormValid) return;
isLoading = true;
errorMessage = "";
try
{
var user = await AdminUserRepository.GetByUsernameAsync(model.Username);
if (user == null || !BCrypt.Net.BCrypt.Verify(model.Password, user.PasswordHash))
{
errorMessage = "사용자명 또는 비밀번호가 올바르지 않습니다.";
isLoading = false;
return;
}
var claims = new List
{
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
new Claim(ClaimTypes.Name, user.Username)
};
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var authProperties = new AuthenticationProperties { IsPersistent = true };
await HttpContextAccessor.HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(claimsIdentity),
authProperties);
NavigationManager.NavigateTo("/taxbaik/admin/dashboard");
}
catch (Exception ex)
{
errorMessage = $"로그인 중 오류: {ex.Message}";
}
finally
{
isLoading = false;
}
}
private class LoginModel
{
public string Username { get; set; }
public string Password { get; set; }
}
}