我創建了一個名爲PasswordHistory
的表。每次用戶更改密碼時,都應將當前密碼複製到PasswordHistory表中。該政策是限制最多的下列2:ASP> NET身份:如何將用戶輸入的密碼與哈希密碼進行比較?
- 用戶不能使用任何的最後8個密碼,
- 或者說,它已經在過去的2年使用的密碼
我會想知道如何比較新輸入的密碼和現有的密碼,但該密碼是散列的?
這裏是我的代碼:
var _limitDate = DateTime.Now.AddYears(-2);
int n = db.PasswordsHistory.Where(pwd => pwd.UserId == userId && pwd.ChangeDate > _limitDate).Count();
var pwdList = new List<PasswordHistory>();
if(n >= 8)
{
pwdList = db.PasswordsHistory
.Where(pwd => pwd.ChangeDate > _limitDate)
.ToList();
}
else
{
pwdList = db.PasswordsHistory
.OrderByDescending(pwd => pwd.ChangeDate)
.Take(8)
.ToList();
}
if (pwdList.Count == 0)
{
return false;
}
else
{
foreach (var pwd in pwdList)
{
//compare the password entered by the user with the password stored in the PasswordHistory table
}
}
感謝您的幫助
PasswordsHistory表包含舊密碼哈希? – tmg