CreateMachineKey方法

public static string CreateMachineKey(int length)
{
    // Create a byte array.
    byte[] random = new byte[length/2];
    // Create a cryptographically strong random number generator.
    RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
    // Fill the byte array with random bytes.
    rng.GetBytes(random);
    // Create a StringBuilder to hold the result once it is
    // converted to hexadecimal format.
    System.Text.StringBuilder machineKey = new
      System.Text.StringBuilder(length);
    // Loop through the random byte array and append each value
    // to the StringBuilder.
    for (int i = 0; i < random.Length; i++)
    {
        machineKey.Append(String.Format("{0:X2}", random[i]));
    }
    return machineKey.ToString();

}

 Along with the validationKey and decryptionKey attributes described so far, you can also
choose the algorithm that’s used to create the view state hash code. The SHA1 algorithm is recom-
mended for the best encryption strength, but you can alternately choose MD5 (Message Digest 5,
which offers better performance), AES (Rijndael), or 3DES (TripleDES). In addition, you can add the
validation attribute to specify what encryption method is used for the login ticket that’s used with
forms authentication. Valid values are AES, DES,
3DES, and Auto (the default, which varies based on the form authentication settings you’re using).

 

 

 

 

posted @ 2009-10-30 13:13  wispzone  阅读(147)  评论(0编辑  收藏  举报