Asp.net中machineKey的生成和作用

machineKey生成代码:

代码
validationKey = CreateKey(20);

decryptionKey
= CreateKey(24);

protected string CreateKey(int len)

{

byte[] bytes = new byte[len];

new RNGCryptoServiceProvider().GetBytes(bytes);

StringBuilder sb
= new StringBuilder();

for(int i = 0; i < bytes.Length; i++)

{

sb.Append(
string.Format("{0:X2}",bytes[i]));

}

return sb.ToString();

}

web.config配置:

代码
xml version="1.0"?>

<configuration>

<system.web>

<machineKey validationKey="3FF1E929BC0534950B0920A7B59FA698BD02DFE8" decryptionKey="280450BB36319B474C996B506A95AEDF9B51211B1D2B7A77" decryption="3DES" validation="SHA1"/>

system.web>

configuration>
 

machineKey作用:

1)在forms authentication tickets认证时加密与解密,防止被篡改。

2)ViewState加密与解密,防止篡改。

3)使用进程外session(out-of-process session)时,对会话状态标识进行验证。

 

posted @ 2010-11-18 13:07  T#S  阅读(1089)  评论(0编辑  收藏  举报