【问题标题】:.Net Membership Provider-- Reset (or Change) Password When Old Password and Q/A are Unknown.Net Membership Provider--当旧密码和 Q/A 未知时重置(或更改)密码
【发布时间】:2013-04-18 07:12:44
【问题描述】:

试图弄清楚以下是否可行。我的设置(我无法更改)是:

<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,但它有效。还是想看看有没有其他选择。如果没有内置的方法来做到这一点,这似乎是提供者的一个缺陷。

【问题讨论】:

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


    【解决方案1】:

    使用 ResetPassword 更新密码

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

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

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

    正如你所说的ChangePassword() 不起作用,这里是会员提供者用来生成盐和散列密码的方法。您可以使用它,并手动将盐和密码保存在数据库中。

    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;
    }
    

    【讨论】:

    • 我不确定我是否理解。您的意思是创建一个临时密码,生成盐和哈希并(手动)保存它们,然后将临时密码用于 ChangePassword?
    • 对不起,我错过了带领你。我更新了答案。它们是两种完全不同的解决方案 - 如果第一种解决方案不起作用,您可以使用第二种解决方案。
    猜你喜欢
    • 2012-05-07
    • 1970-01-01
    • 1970-01-01
    • 2011-06-28
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多