【发布时间】:2014-03-25 22:06:35
【问题描述】:
我有一个现有的会员数据库,其中密码是用户名和唯一 ID 的哈希值。据我了解,ASP.NET Identity 会为您处理密码。
但是,在更新之前,我需要使用旧的散列密码(即它们需要在第一次登录时使用,然后我会更新它)。
IPasswordHasher 有方法:VerifyHashedPassword(string hashedPassword, string providedPassword)。这种方法不允许我传递任何盐。我意识到我不需要为任何 新 散列密码提供值,但对于我现有的密码,我需要进行遗留检查。
public class CoolGuyPasswordHasher : PasswordHasher {
public IdentityContext DbContext { get; set; }
// Custom hashing used before migrating to Identity
public static string GetSHA1Hash(string password, string guid) {
string passWithSalt = String.Concat(password, guid);
return FormsAuthentication.HashPasswordForStoringInConfigFile(passWithSalt, "SHA1");
}
// Verify if the password is hashed using SHA1. If yes, rehash using ASP.NET Identity Crypto which is more secure
public override PasswordVerificationResult VerifyHashedPassword(string hashedPassword, string providedPassword) {
//I can't pass in my salt!
if (String.Equals(hashedPassword, GetSHA1Hash(providedPassword, wheresTheSalt), StringComparison.InvariantCultureIgnoreCase)) {
ReHashPassword(hashedPassword, providedPassword);
return PasswordVerificationResult.Success;
}
return base.VerifyHashedPassword(hashedPassword, providedPassword);
}
}
在上面的代码 sn-p 中,请注意在对 GetSHA1Hash 的调用中我没有第二个参数要传入。
我该如何进行旧式加盐密码检查?在新系统中,我想我可以让散列密码保留为用户名 + id 的结果。但是,由于 ASP.NET Identity 的实现似乎并不能满足这一点,我最好的选择是什么?
【问题讨论】: