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

前端 未结 4 1466
执笔经年
执笔经年 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 13:02

    Quick dirty method

    Public Shared Function GetSaltKey() As String
                Dim saltBytes() As Byte
                Dim minSaltSize As Integer = 4
                Dim maxSaltSize As Integer = 8
    
                ' Generate a random number for the size of the salt.
                Dim random As Random
                random = New Random()
    
                Dim saltSize As Integer
                saltSize = random.Next(minSaltSize, maxSaltSize)
    
                ' Allocate a byte array, which will hold the salt.
                saltBytes = New Byte(saltSize - 1) {}
    
                ' Initialize a random number generator.
                Dim rng As RNGCryptoServiceProvider
                rng = New RNGCryptoServiceProvider()
    
                ' Fill the salt with cryptographically strong byte values.
                rng.GetNonZeroBytes(saltBytes)
    
                ' Convert plain text into a byte array.
                Return Convert.ToBase64String(saltBytes)
            End Function
    
            Public Shared Function ComputeHash(ByVal password As String, ByVal salt As String) As String
    
                Return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(salt & password, _
                    System.Web.Configuration.FormsAuthPasswordFormat.SHA1.ToString)
            End Function
    

    Although, the membership namespace has stuff built in for this as well, as stated by Forgotten Semicolon

提交回复
热议问题