使用 System.Web.Providers.DefaultMembershipProvider 和 System.Web.Security.MembershipUser,我试图更改数据库中的用户密码。出于某种原因,无论我做什么, MembershipUser.ChangePassword(old, new) 都会返回 false 且没有错误。
在数据库中,与应用程序名称关联的 ApplicationID 是正确的,并且我的用户在 Memberships 和 Users 表中都附加了该 ApplicationID。就这一点而言,用户不为空,对象中的所有数据都是准确的。我在这里完全不知所措,任何帮助将不胜感激,因为没有其他来源能够帮助我。
EDIT:
User is not locked out, the user is active, and the password is correct. All
password requirements are being fulfilled. This is extremely frustrating since
the MembershipUser.GetUser() finds the user and all of its associated data but
will not change the password.
Additional Steps:
- Tried MembershipUser.UnlockUser() just in case, no luck.
- Tried MembershipUser.ResetPassword() then a change. This fails with the
error message "Value cannot be null" even though it should be able to.
- GetSpecificVerion of the entire solution and working to when it first
broke. Every version worked, including my latest (Yay!), but then it
magically stopped working again a few minutes later, no code changes
were made at all.
Bounty added, looking for any and all possibilities that could lead to a fix...
配置文件:
----
<add name="aspnetdb" connectionString="Application Name=appname;Type System Version=SQL Server 2008;server=***,****;Initial Catalog=aspnetdb;Integrated Security=false;pwd=****;user id=****" providerName="System.Data.SqlClient" />
----
----
<membership defaultProvider="DefaultMembershipProvider">
<providers>
<add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="aspnetdb" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" passwordAttemptWindow="10" applicationName="mysite.com" />
</providers>
</membership>
----
方法:
public ActionResult ChangePassword(ChangePasswordViewModel model)
{
// Indicates whether changing the password was successful or not.
Boolean passwordChanged = false;
if (ModelState.IsValid)
{
// Grab the current user.
MembershipUser user = Membership.GetUser(User.Identity.Name, true);
if (user != null)
{
passwordChanged = user.ChangePassword(model.OldPassword, model.NewPassword);
}
}
return Json(new { Success = passwordChanged });
}