ASP.NET Core Identity 2FA 토큰 생성 및 토큰 로그인
ASP.NET Core Identity에서 2FA 토큰 생성 및 생성된 토큰으로 로그인하는 코드 조각을 보여줍니다.
LoginWith2fa.cshtml
public async Task<IActionResult> OnGetAsync(bool rememberMe, string returnUrl = null)
{
// Ensure the user has gone through the username & password screen first
var user = await _signInManager.GetTwoFactorAuthenticationUserAsync();
// 토큰 생성
var token = await _userManager.GenerateTwoFactorTokenAsync(user, "Email");
return Page();
}
LoginWith2fa.cshtml.cs
public async Task<IActionResult> OnPostAsync(bool rememberMe, string returnUrl = null)
{
if (!ModelState.IsValid)
{
return Page();
}
returnUrl = returnUrl ?? Url.Content("~/");
var user = await _signInManager.GetTwoFactorAuthenticationUserAsync();
var authenticatorCode = Input.TwoFactorCode.Replace(" ", string.Empty).Replace("-", string.Empty);
// 토큰 확인 후 로그인
var result = await _signInManager.TwoFactorSignInAsync(
"Email", authenticatorCode, rememberMe, Input.RememberMachine);
if (result.Succeeded)
{
_logger.LogInformation("User with ID '{UserId}' logged in with 2fa.", user.Id);
return LocalRedirect(returnUrl);
}
}
Comments
Comments are closed