How to create a asp.net membership provider hashed password manually?

前端 未结 4 1471
执笔经年
执笔经年 2020-12-08 12:36

I\'m using a website as a frontend and all users are authenticated with the standard ASP.NET Membership-Provider. Passwords are saved \"hashed\" within a SQL-Database.

4条回答
  •  暖寄归人
    2020-12-08 12:56

    I used reflector to take a look at those methods the .NET-Framework is using internal. Maybe there are public methods available for this but I did not find them - if you know how to query those internal methods as a user please left a comment! :)

    Here is the simplified source-code without unnecessary conditions because I only want to encode the password as a SHA1-Hash:

    private string GenerateSalt() {
      var buf = new byte[16];
      (new RNGCryptoServiceProvider()).GetBytes(buf);
      return Convert.ToBase64String(buf);
    }
    
    private string EncodePassword(string pass, string salt) {
        byte[] bytes = Encoding.Unicode.GetBytes(pass);
        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("SHA1");
        inArray = algorithm.ComputeHash(dst);
        return Convert.ToBase64String(inArray);
    }
    

提交回复
热议问题