如何检查密码是否有效?ASP.. NET MVC成员
本文关键字:有效 ASP NET 成员 MVC 是否 密码 何检查 检查 | 更新日期: 2023-09-27 17:50:00
我使用默认的asp.net mvc 4成员系统。用户通过ASP发送他的用户名和密码。纯文本的。NET Web API。
所以我有他的纯密码,如何比较它与存储哈希密码?
是否有一个函数接受字符串并将其与散列字符串进行比较?
你必须确保你的网页。配置已正确设置为使用成员关系。http://msdn.microsoft.com/en-us/library/6e9y4s5t%28v=vs.100%29.aspx
另外,我确保在您的web中创建一个MachineKey。配置也是如此。http://msdn.microsoft.com/en-us/library/ff649308.aspx
您将在控制器中写入的代码将类似于:
[HttpPost]
public ActionResult Login(AuthenticationModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.Username, model.Password)) {
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/''"))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
}
}
您的模型类似于:
public class AuthenticationModel
{
[Required]
[Display(Name = "Username")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Remember Me?")]
public bool RememberMe { get; set; }
}
我有一个类似的请求,我完成的是使用64字节字段存储密码然后生成32字节的salt和32字节的哈希,然后从DB中提取salt并使用该salt编码相同的用户名如果结果对象等于DB中的
这是我使用的方法
public static bool IsPasswordValid(string plainPassword, byte[] data)
{
var prf = KeyDerivationPrf.HMACSHA512;
var saltBytes = new byte[saltSize];
var hashBytes = new byte[hashSize];
Array.Copy(data, 0, saltBytes, 0, saltSize);
Array.Copy(data, saltSize, hashBytes, 0, hashSize);
var verificationHashBytes = KeyDerivation.Pbkdf2(plainPassword, saltBytes, prf, iterationCount, hashSize);
return hashBytes.SequenceEqual(verificationHashBytes);
}