.Net会员提供商 - 旧密码和Q / A未知时重置(或更改)密码

时间:2013-04-24 16:10:35

标签: asp.net xml asp.net-membership

试图弄清楚以下是否可行。我的设置(我没有选择更改)是:

<add [...]
  enablePasswordRetrieval="false"
  enablePasswordReset="true"
  requiresQuestionAndAnswer="true"
  applicationName="/"
  requiresUniqueEmail="false"
  passwordFormat="Hashed"
  maxInvalidPasswordAttempts="5"
  minRequiredPasswordLength="7"
  minRequiredNonalphanumericCharacters="0"
  passwordAttemptWindow="10"
  passwordStrengthRegularExpression="" />

我不能在没有旧密码的情况下调用ChangePassword(),但我也无法通过user.ResetPassword()设置临时密码,因为我收到一条错误,指出需要提供passwordAnswer。

我有哪些更改用户密码的选项?

修改:找到了这个:

Change Password Issue in AspNet MembershipProvider

Hacky但是它有效。还是想看看是否还有其他选择。如果没有内置的方法,这似乎是提供者中的一个缺陷。

1 个答案:

答案 0 :(得分:0)

使用ResetPassword更新密码

您可以使用这个简单的技巧重置密码。通过重置用户密码获取临时密码。然后通过将临时密码作为旧密码传递来更改所需的密码。

var user = Membership.GetUser(Username, false);
var tempPassword = user.ResetPassword();
user.ChangePassword(tempPassword, NewPassword);

另一种方式:手动更新密码

正如您所说ChangePassword()不起作用,以下是成员资格提供程序用于生成salt和哈希密码的方法。您可以使用它,并手动将数据库和密码保存在数据库中。

private static string GenerateSalt()
{
    byte[] numArray = new byte[16];
    (new RNGCryptoServiceProvider()).GetBytes(numArray);
    string base64String = Convert.ToBase64String(numArray);
    return base64String;
}

private string EncodePassword(string pass, int passwordFormat, string salt)
{
    byte[] numArray;
    byte[] numArray1;
    string base64String;
    bool length = passwordFormat != 0;
    if (length)
    {
        byte[] bytes = Encoding.Unicode.GetBytes(pass);
        byte[] numArray2 = Convert.FromBase64String(salt);
        byte[] numArray3 = null;

        HashAlgorithm hashAlgorithm = HashAlgorithm.Create(Membership.HashAlgorithmType);

        if (hashAlgorithm as KeyedHashAlgorithm == null)
        {
            numArray1 = new byte[(int) numArray2.Length + (int) bytes.Length];
            Buffer.BlockCopy(numArray2, 0, numArray1, 0, (int) numArray2.Length);
            Buffer.BlockCopy(bytes, 0, numArray1, (int) numArray2.Length, (int) bytes.Length);
            numArray3 = hashAlgorithm.ComputeHash(numArray1);
        }
        else
        {
            KeyedHashAlgorithm keyedHashAlgorithm = (KeyedHashAlgorithm) hashAlgorithm;
            if (keyedHashAlgorithm.Key.Length != numArray2.Length)
            {

                if (keyedHashAlgorithm.Key.Length >= (int) numArray2.Length)
                {
                    numArray = new byte[(int) keyedHashAlgorithm.Key.Length];
                    int num = 0;
                    while (true)
                    {
                        length = num < (int) numArray.Length;
                        if (!length)
                        {
                            break;
                        }
                        int num1 = Math.Min((int) numArray2.Length, (int) numArray.Length - num);
                        Buffer.BlockCopy(numArray2, 0, numArray, num, num1);
                        num = num + num1;
                    }
                    keyedHashAlgorithm.Key = numArray;
                }
                else
                {
                    numArray = new byte[(int) keyedHashAlgorithm.Key.Length];
                    Buffer.BlockCopy(numArray2, 0, numArray, 0, (int) numArray.Length);
                    keyedHashAlgorithm.Key = numArray;
                }
            }
            else
            {
                keyedHashAlgorithm.Key = numArray2;
            }
            numArray3 = keyedHashAlgorithm.ComputeHash(bytes);
        }

        base64String = Convert.ToBase64String(numArray3);
    }
    else
    {
        base64String = pass;
    }
    return base64String;
}