ASP.NET Core MVC에서 90일 암호 만료 기능 구현하기
추천 자료: ASP.NET Core 인증 및 권한 부여
ASP.NET Core MVC에서 90일 암호 만료 기능 구현하기
서론
ASP.NET Core Identity는 강력한 보안 기능을 제공하지만, 특정 비즈니스 요구 사항을 충족하기 위해서는 추가적인 사용자 정의가 필요할 때가 많습니다. 이 아티클에서는 ASP.NET Core MVC 애플리케이션에 사용자 암호의 90일 만료 기능을 구현하는 방법을 안내합니다.
사용자 모델 확장
먼저, ApplicationUser
클래스에 DateTimePasswordUpdated
속성을 추가합니다. 이는 사용자의 마지막 암호 변경 시간을 추적하는 데 사용됩니다.
public DateTimeOffset? DateTimePasswordUpdated { get; set; }
로그인 컨트롤러 수정
로그인 프로세스에 암호 만료 검사를 추가합니다. 사용자가 로그인을 시도할 때, 시스템은 먼저 암호가 만료되었는지를 확인합니다.
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{
// ... 기존 로그인 로직 ...
if (this.PasswordHasExpired(user, model.Password))
{
this.ModelState.AddModelError(string.Empty, "Your password has expired. Please use \"forgot password\" to reset your password.");
return this.View(model);
}
// ... 기존 로그인 로직 계속 ...
}
암호 만료 검사 메서드
PasswordHasExpired
메서드는 사용자의 암호가 만료되었는지 확인합니다. 이 메서드는 사용자의 DateTimePasswordUpdated
속성과 현재 날짜를 비교하여, 지정된 기간(90일)이 초과했는지 여부를 결정합니다.
#region Password Expiration Check Method
private bool PasswordHasExpired(ApplicationUser appUser, string rawPsw)
{
// ... 메서드 구현 ...
}
#endregion
참고용 소스:
#region Password Expiration Check Method
/// <summary>
/// Checks if the password of the given ApplicationUser has expired.
/// </summary>
/// <param name="appUser">The ApplicationUser object to check.</param>
/// <param name="rawPsw">The raw password provided by the user.</param>
/// <returns>True if the password has expired; otherwise, false.</returns>
private bool PasswordHasExpired(ApplicationUser appUser, string rawPsw)
{
// Verify if the provided raw password matches the hashed password stored for the user.
if (this.userManager.PasswordHasher.VerifyHashedPassword(appUser, appUser.PasswordHash, rawPsw) == PasswordVerificationResult.Success)
{
// Check if the current date and time is greater than the DateTimePasswordUpdated of the user.
// If the difference between the current date and DateTimePasswordUpdated is greater than or equal to 90 days, return true indicating the password has expired.
return (DateTimeOffset.UtcNow > appUser.DateTimePasswordUpdated) && (DateTimeOffset.UtcNow - appUser.DateTimePasswordUpdated >= TimeSpan.FromDays(90));
}
// If the password verification fails or the password has not expired, return false.
return false;
}
#endregion
결론
이 아티클에서는 ASP.NET Core Identity를 사용하여 사용자의 암호 만료 기능을 구현하는 방법을 살펴보았습니다. 이 기능은 보안을 강화하고, 사용자의 암호를 주기적으로 변경하도록 유도하는 데 도움이 됩니다. 이러한 사용자 정의 기능은 애플리케이션의 보안을 높이는 데 중요한 역할을 합니다.
참고
본 아티클에서 제시된 코드는 예시일 뿐이며, 실제 애플리케이션에 적용하기 전에 프로젝트의 특정 요구 사항에 맞게 조정해야 합니다.
추천 자료: .NET Blazor에 대해 알아보시겠어요? .NET Blazor 알아보기를 확인해보세요!