我继承了一个用MVC编写的系统。该系统使用asp.net成员资格api,效果很好。我刚刚发现了一个错误,但是用户无法更改他/她的密码。
系统显示输入旧密码的表格,新密码两次确认,但是点击提交时,它只是重新显示表格,不会更改密码。
我已经完成了代码但由于我对MVC足够新,并且使用了成员资格api,我看不出它有什么过分错误。
以下是帐户控制器中的GET和POST代码。如果有人能看到这个有什么问题,我会非常感激。如果有人需要我发布其他信息/代码,请询问:)
此外,在通过代码调试之后,似乎发生了什么,在
之后if (ModelState.IsValid)
点击,跳过此中的嵌套if语句,代码直接跳到底部重新显示表单。
[Authorize]
public ActionResult ChangePassword(string source)
{
ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
ViewData["source"] = source;
if (!string.IsNullOrEmpty(source))
{
return View("ChangePassword", source);
}
return View("ChangePassword", "User");
}
[Authorize]
[HttpPost]
public ActionResult ChangePassword(ChangePasswordModel model, FormCollection formValues)
{
string source = formValues["source"];
if (formValues["btnCancel"] != null)
{
RedirectToRouteResult result = null;
// The user has clicked cancel. Redirect back to source!
//
switch (source)
{
case "user":
result = RedirectToAction("Index", "ManageAccount", new { Area = "User" });
break;
case "administrator":
result = RedirectToAction("Index", "ManageAccount", new { Area = "Administrator" });
break;
}
return result;
}
if (ModelState.IsValid)
{
if (MembershipService.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword))
{
return RedirectToAction("Index", "ManageAccount", new { Area = "User" });
}
else
{
ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
}
}
// If we got this far, something failed, redisplay form
ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
ViewData["source"] = source;
return View("ChangePassword", formValues["source"], model);
}