如何重置和在MVC中的asp.net成员资格提供程序中更改哈希密码 [英] how to reset & change the hash password in asp.net membership provider in MVC

查看:81
本文介绍了如何重置和在MVC中的asp.net成员资格提供程序中更改哈希密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到了代码:

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

我不明白我会怎么做由于用户忘记了旧密码,因此获得了客户端密码.我想添加一个重置功能,该功能将生成一个随机密码,然后向特定的客户端发送一封电子邮件,其中将包含用户名和随机生成的密码.在他/她可以更改密码之后.

I dont understand how I will get the client password as the user has forgotten his old password. I want to add a reset functionality which would generate a random password and send an email to the particular client which will have the userid and the random generated password. After he/she would be able to change the password.

推荐答案

您可以使用如果您需要创建自己的salt并哈希一个新密码,则此实现与会员代码的作用大致相同:

If you need to create your own salt and hash a new password, here is an implementation which does much the same as the membership code:

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
}

这篇关于如何重置和在MVC中的asp.net成员资格提供程序中更改哈希密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆