0
我是Adam Freeman書籍的忠實粉絲。在他的Pro Asp.net mvc 5平臺的第13章第325頁中,下面的代碼讓我困惑。有沒有人有解釋爲什麼他明確地使用電子郵件和密碼驗證?Microsoft.AspNet.Identity中的顯式密碼和電子郵件驗證,爲什麼需要?
呼叫this.UserManager.UpdateAsync(user)
應返回this.UserManager.UserValidator.ValidateAsync(user)
和this.UserManager.PasswordValidator.ValidateAsync(password)
生成的相同錯誤的結果。他不是兩次做同樣的事嗎?或者有一個特殊的目的?
[HttpPost]
public async Task<ActionResult> Edit(string id, string email, string password)
{
AppUser user = await this.UserManager.FindByIdAsync(id);
if (user != null)
{
user.Email = email;
IdentityResult validEmail = await this.UserManager.UserValidator.ValidateAsync(user);
if (!validEmail.Succeeded)
{
this.AddErrorsFromResult(validEmail);
}
IdentityResult validPass = null;
if (password != string.Empty)
{
validPass = await this.UserManager.PasswordValidator.ValidateAsync(password);
if (validPass.Succeeded)
{
user.PasswordHash = this.UserManager.PasswordHasher.HashPassword(password);
}
else
{
this.AddErrorsFromResult(validPass);
}
}
if ((validEmail.Succeeded && validPass == null)
|| (validEmail.Succeeded && password != string.Empty && validPass.Succeeded))
{
IdentityResult result = await this.UserManager.UpdateAsync(user);
if (result.Succeeded)
{
return this.RedirectToAction("Index");
}
this.AddErrorsFromResult(result);
}
}
else
{
ModelState.AddModelError(string.Empty, "User not found");
}
return this.View(user);
}
private AppUserManager UserManager
{
get
{
return HttpContext.GetOwinContext().GetUserManager<AppUserManager>();
}
}
private void AddErrorsFromResult(IdentityResult result)
{
foreach (string error in result.Errors)
{
ModelState.AddModelError(string.Empty, error);
}
}
嗯,唯一真正能夠回答的人是亞當弗里曼,不是嗎?很可能,在書中有一些聯繫信息,所以我建議直接與他聯繫。 –