如何在MVC的asp.net会员提供者中重置和改变哈希密码?

 2023-02-16    353  

问题描述

我来了代码:

MembershipUser u = Membership.GetUser();
u.ChangePassword(u.ResetPassword(), "Password"); //where will I get the "Password" from

我不明白我是怎么回事
获取客户端密码,因为用户忘记了他的旧密码.
我想添加一个重置功能,它会产生随机密码和
向特定客户端发送电子邮件,该客户端将具有UserID和随机生成的密码.他/她能够更改密码后.

如何在MVC的asp.net会员提供者中重置和改变哈希密码?

推荐答案

您可以使用成员身份磁点方法

string password = System.Web.Security.Membership.GeneratePassword(14, 0);

如果您需要创建自己的盐和哈希新密码,这里是一个与成员资格代码相同的实现:

public class Cryptographer : ICryptographer
{
    #region ICryptographer Members

    public string CreateSalt()
    {
        byte[] data = new byte[0x10];
        new RNGCryptoServiceProvider().GetBytes(data);
        return Convert.ToBase64String(data);
    }

    /// <summary>
    /// Hash the password against the salt
    /// </summary>
    /// <param name="pass">Plain password</param>
    /// <param name="salt">Salt string</param>
    /// <returns>Encrypted password</returns>
    public string HashPassword(string password, string salt)
    {
        byte[] bytes = Encoding.Unicode.GetBytes(password);
        byte[] src = Convert.FromBase64String(salt);
        byte[] dst = new byte[src.Length + bytes.Length];
        byte[] inArray = null;
        Buffer.BlockCopy(src, 0, dst, 0, src.Length);
        Buffer.BlockCopy(bytes, 0, dst, src.Length, bytes.Length);
        HashAlgorithm algorithm = HashAlgorithm.Create(System.Web.Security.Membership.HashAlgorithmType);
        inArray = algorithm.ComputeHash(dst);
        return Convert.ToBase64String(inArray);
    }

    #endregion
}

其他推荐答案

ChangePassword方法的第二个额外计是转售您想要为该用户使用的新密码的字符串.

您可以将其更改为您想要的任何字符串,甚至是您将发电子邮件给用户的自动生成的字符串.

更新

要回答您的新问题,我相信密码等的所有散列都由会员提供商处理.

如果您只想将用户密码重置为随机的新值,您可能会使用ResetPassword方法而不是ChangePassword更好.

这将:

将用户的密码重置为新的自动生成的密码.

以上所述是小编给大家介绍的如何在MVC的asp.net会员提供者中重置和改变哈希密码?,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/34072.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。