Asp.net

如何手動創建 asp.net 會員提供者散列密碼?

  • March 31, 2010

我使用網站作為前端,所有使用者都通過標準 ASP.NET Membership-Provider 進行身份驗證。密碼以“散列”形式保存在 SQL 數據庫中。

現在我想編寫一個具有管理功能的桌面客戶端。除其他外,應該有一種方法可以重置使用者密碼。我可以使用保存的會員數據訪問數據庫,但是如何手動創建密碼鹽和雜湊?使用 System.Web.Membership 命名空間似乎不合適,所以我需要知道如何手動創建新密碼的 salt 和 hash。

專家站出來!:)

您絕對可以System.Web.Security在控制台或 winforms 應用程序中使用。

這是一個簡單的控制台應用程序:

static void Main(string[] args)
{
   MembershipProvider provider = Membership.Provider;

   MembershipUser myUser = provider.GetUser("myUser", false);

   if( myUser != null ) provider.DeleteUser("myUser", true);

   MembershipCreateStatus status;

   myUser = provider.CreateUser("myUser", "password", "user@example.com", null, null, true, null, out status);

   if (status != MembershipCreateStatus.Success)
   {
       Console.WriteLine("Could not create user.  Reason: " + status.ToString());
       Console.ReadLine();
       return;
   }

   Console.WriteLine("Authenticating with \"password\": " + provider.ValidateUser("myUser", "password").ToString());

   string newPassword = myUser.ResetPassword();

   Console.WriteLine("Authenticating with \"password\": " + provider.ValidateUser("myUser", "password").ToString());
   Console.WriteLine("Authenticating with new password: " + provider.ValidateUser("myUser", newPassword).ToString());

   Console.ReadLine();
}

和 app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <connectionStrings>
       <add name="MyConnectionString" connectionString="Data Source=localhost;Initial Catalog=MyDatabase;Integrated Security=True" providerName="System.Data.SqlClient" />
   </connectionStrings>
   <system.web>
       <membership defaultProvider="MyMembershipProvider">
           <providers>
               <clear />
               <add name="MyMembershipProvider"
                    type="System.Web.Security.SqlMembershipProvider"
                    connectionStringName="MyConnectionString"
                    applicationName="MyApplication"
                    minRequiredPasswordLength="5"
                    minRequiredNonalphanumericCharacters="0"
                    requiresQuestionAndAnswer="false" />
           </providers>
       </membership>
   </system.web>
</configuration>

我使用反射器來查看 .NET-Framework 內部使用的那些方法。也許有可用的公共方法,但我沒有找到它們 - 如果您知道如何以使用者身份查詢這些內部方法,請留下評論!:)

這是沒有不必要條件的簡化原始碼,因為我只想將密碼編碼為 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);
}

引用自:https://stackoverflow.com/questions/2547397