gpt4 book ai didi

c# - ASP.NET 成员更改密码不起作用

转载 作者:可可西里 更新时间:2023-11-01 08:18:33 24 4
gpt4 key购买 nike

我有这段代码可以在用户单击密码重置按钮时更改用户的密码(有额外的代码可以登录 ELMAH,这样我就可以尝试找出问题所在)。

这是在 ASP.NET MVC 2 中,使用标准的 aspnet 成员提供程序,带有一个像这样的简单 View :

New Password:     ______
Confirm Password: ______
[Reset] [Cancel]

这个 View 的路由是/Account/Reset/guid,其中guid是用户在aspnet成员数据库中的id。

代码的关键部分是它调用 user.ChangePassword() 的地方。您可以看到它在成功时记录了一条消息。问题是对于某些用户,记录了成功消息,但他们无法使用新密码登录。对于其他用户,它会记录成功消息并且他们可以登录。

if (user.ChangePassword(pwd, confirmPassword))
{
ErrorSignal.FromCurrentContext().Raise(
new Exception("ResetPassword - changed successfully!"));
return Json(new {
Msg = "You have reset your password successfully." },
JsonRequestBehavior.AllowGet);
}

完整的代码 list 是:

[HttpPost]
public JsonResult ResetPassword(string id, string newPassword, string confirmPassword)
{
ErrorSignal.FromCurrentContext().Raise(new Exception("ResetPassword started for " + id));

ViewData["PasswordLength"] = Membership.MinRequiredPasswordLength;

if (string.IsNullOrWhiteSpace(newPassword))
{
ErrorSignal.FromCurrentContext().Raise(
new Exception("ResetPassword - new password was blank."));
ModelState.AddModelError("_FORM", "Please enter a new password.");
return Json(new { Errors = ModelState.Errors() }, JsonRequestBehavior.AllowGet);
}

if (newPassword.Length < Membership.MinRequiredPasswordLength)
{
ErrorSignal.FromCurrentContext().Raise(
new Exception("ResetPassword - new password was less than minimum length."));
ModelState.AddModelError("_FORM",
string.Format("The password must be at least {0} characters long.",
Membership.MinRequiredPasswordLength));
return Json(new { Errors = ModelState.Errors() }, JsonRequestBehavior.AllowGet);
}

if (string.IsNullOrWhiteSpace(confirmPassword))
{
ErrorSignal.FromCurrentContext().Raise(
new Exception("ResetPassword - confirm password was blank."));
ModelState.AddModelError("_FORM",
"Please enter the same new password in the confirm password textbox.");
return Json(new { Errors = ModelState.Errors() }, JsonRequestBehavior.AllowGet);
}

if (confirmPassword.Length < Membership.MinRequiredPasswordLength)
{
ErrorSignal.FromCurrentContext().Raise(
new Exception("ResetPassword - confirm password was less than minimum length."));
ModelState.AddModelError("_FORM",
string.Format("The password must be at least {0} characters long.",
Membership.MinRequiredPasswordLength));
return Json(new { Errors = ModelState.Errors() }, JsonRequestBehavior.AllowGet);
}

if (confirmPassword != newPassword)
{
ErrorSignal.FromCurrentContext().Raise(
new Exception("ResetPassword - new password did not match the confirm password."));
ModelState.AddModelError("_FORM", "Please enter the same password again.");
return Json(new { Errors = ModelState.Errors() }, JsonRequestBehavior.AllowGet);
}

bool isMatch = ValidationHelper.IsGUID(id);
if (string.IsNullOrWhiteSpace(id) || !isMatch)
{
ErrorSignal.FromCurrentContext().Raise(
new Exception("ResetPassword - id was not a guid."));
ModelState.AddModelError("_FORM", "An invalid ID value was passed in through the URL");
}
else
{
//ID exists and is kosher, see if this user is already approved
//Get the ID sent in the querystring
Guid userId = new Guid(id);

try
{
//Get information about the user
MembershipUser user = Membership.GetUser(userId);
if (user == null)
{
//could not find the user
ErrorSignal.FromCurrentContext().Raise(
new Exception("ResetPassword - could not find user by id " + id));
ModelState.AddModelError("_FORM",
"The user account can not be found in the system.");
}
else
{
ErrorSignal.FromCurrentContext().Raise(
new Exception("ResetPassword - user is " + user.UserName));
string pwd = user.ResetPassword();

if (user.ChangePassword(pwd, confirmPassword))
{
ErrorSignal.FromCurrentContext().Raise(
new Exception("ResetPassword - changed successfully!"));
return Json(new {
Msg = "You have reset your password successfully." },
JsonRequestBehavior.AllowGet);
}
ErrorSignal.FromCurrentContext().Raise(
new Exception("ResetPassword
- failed to change the password, for an unknown reason"));
}
}
catch (Exception ex)
{
ErrorSignal.FromCurrentContext().Raise(
new Exception("ResetPassword: " + ex));
return Json(new { Error = ex.Message + " -> "
+ ex.InnerException.Message }, JsonRequestBehavior.AllowGet);
}
}

return Json(new { Errors = ModelState.Errors() }, JsonRequestBehavior.AllowGet);
}

编辑:添加赏金以尝试解决此问题。这是我的问题列表中最烦人的问题之一,我不知道如何继续。

最佳答案

如果用户需要重设密码,他们的帐户有可能因无效尝试次数过多而被锁定。如果是这种情况,则密码重置成功,但在锁定条件清除之前用户无法登录。

尝试检查 MembershipUser.IsLockedOut :

Users are most commonly locked out and cannot be validated by the ValidateUser method when the MaxInvalidPasswordAttempts is reached within the PasswordAttemptWindow.

To set this property to false and let the user try to log in again, you can use the UnlockUser method.

编辑

你有没有检查IsApproved ?如果用户的 false 身份验证将失败。

此外,假设默认情况下的成员资格提供程序,您指的是 SqlMembershipProvider,您可以对您的数据库运行以下查询并确保一切看起来正确吗?

select IsApproved, IsLockedOut, FailedPasswordAttemptCount
from aspnet_Membership
where ApplicationId = @yourApplicationId and UserId = @userId

在尝试登录之前尝试执行查询以验证 IsApprovedIsLockedOut 是否正常。另请注意 FailedPasswordAttemptCount 的值。

尝试登录,然后再次运行查询。如果登录失败,FailedPasswordAttemptCount 的值是否已递增?

您还可以查看 aspnet_Membership 表中的 PasswordFormat 并确保它是正确的值,具体取决于您使用的格式(0 表示清除,1 表示散列,2 表示加密)。

关于c# - ASP.NET 成员更改密码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5048460/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com