2009-02-13 45 views
9

支持我創造一個會員供應商,改變了我的web.config散列或加密的口令不自動生成鍵

<membership defaultProvider="MyMembershipProvider"> 
    <providers> 
    <clear/> 
    <add name="MyMembershipProvider" 
      type="Khafan.Providers.SqlMembershipProvider" 
      connectionStringName="KhafanConnectionString" 
      maxInvalidPasswordAttempts="5"    
      passwordAttemptWindow="10" 
      minRequiredNonalphanumericCharacters="0" 
      minRequiredPasswordLength="4" 
      passwordStrengthRegularExpression="" 
      passwordFormat="Hashed" 
      enablePasswordReset="true" 
      enablePasswordRetrieval="false"    
      requiresQuestionAndAnswer="false" 
      requiresUniqueEmail="true" /> 
    </providers> 
</membership> 

但現在,每當我試圖瀏覽到ASP.Net的安全頁面配置它給了我下面的錯誤:

哈希或加密的口令不自動生成鍵

在我的數據庫我用的身份爲我的PK支持。我不知道這是否是問題。但如果是這樣,我該如何解決它?我不想更改身份值。

謝謝。

回答

19

這是因爲你哈希密碼,但沒有在你的web.config中設置特定的鍵。還有在這個MSDN article一個「密鑰生成器」代碼段,運行它兩次,並在你的web.config推他們爲:

<system.web> 
    <machineKey 
    validationKey="<blah>"   
    decryptionKey="<blah>" 
    validation="SHA1" 
    decryption="AES" 
    /> 

,並應你理清。這是這樣的,否則你可以將你的會員資料數據庫/應用程序到另一臺機器,並沒有你的密碼將工作,因爲自動生成的機器密鑰會不同:-)

+3

或者只是使用這個在線工具:HTTP://www.developmentnow。 com/articles/machinekey_generator.aspx – 2010-07-12 12:17:22

3

是一個schlep尋找在他的回答中提到的「密鑰生成器」片段中提到了 MSDN link 012,因此我將其添加到此處以供快速參考。所以這不是一個獨立的答案。這是對接受答案的補充。

從MSDN

The following code shows how to generate random key values. Compile the code to create a console application, and then pass the required key size as a command line argument expressed as the desired number of hexadecimal characters. Each byte is represented by two hexadecimal characters; therefore, to request a 32-byte key, pass 64 as a command line argument. If you do not specify an argument, the code returns a 128 hexadecimal character (64-byte) key.

using System; 
using System.Text; 
using System.Security; 
using System.Security.Cryptography; 

class App { 
    static void Main(string[] argv) { 
    int len = 128; 
    if (argv.Length > 0) 
     len = int.Parse(argv[0]); 
    byte[] buff = new byte[len/2]; 
    RNGCryptoServiceProvider rng = new 
          RNGCryptoServiceProvider(); 
    rng.GetBytes(buff); 
    StringBuilder sb = new StringBuilder(len); 
    for (int i=0; i<buff.Length; i++) 
     sb.Append(string.Format("{0:X2}", buff[i])); 
    Console.WriteLine(sb); 
    } 
} 

此外,<machineKey>去的<system.web>裏面,像這樣:

<system.web> 
    <machineKey 
     validationKey="" 
     decryptionKey="" 
     validation="SHA1" 
     decryption="AES" 
/>