ASP.NET中设置MachineKey可以很轻松的实现SSO,可以在所有ASP.NET站点中添加如下配置:
<machineKey validationKey="XXXXXX" decryptionKey="XXX" validation="SHA1" />
validationKey可以为视图状态、身份验证Cookie、Session等重要的信息添加杂乱信息以防止重要信息被篡改。
为了防止validationKey和decryptionKey以明文的方式进行显示,可以使用ProtectSection方法对machineKey配置节进行加密。
1、在Web.config中添加原始的配置,如:
<machineKey validationKey="XXXXXX" decryptionKey="XXX" validation="SHA1" />
2、通过程序对system.web/machineKey节进行加密和解密
加密方式如下:
加密MachineKey            Configuration config = WebConfigurationManager.OpenWebConfiguration("/");
加密MachineKey            ConfigurationSection machineKeySection 
= config.GetSection("system.web/machineKey");
加密MachineKey            machineKeySection.SectionInformation.ProtectSection(
"RSAProtectedConfigurationProvider");
加密MachineKey            machineKeySection.SectionInformation.ForceSave 
= true;
加密MachineKey            config.Save();
解密方式如下:
加密MachineKey            Configuration config = WebConfigurationManager.OpenWebConfiguration("/");
加密MachineKey            ConfigurationSection machineKeySection 
= config.GetSection("system.web/machineKey");
加密MachineKey            machineKeySection.SectionInformation.UnprotectSection();
加密MachineKey            machineKeySection.SectionInformation.ForceSave 
= true;
3、通过程序加密就会得到类拟:
加密MachineKey        <machineKey configProtectionProvider="RsaProtectedConfigurationProvider">
加密MachineKey            
<EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
加密MachineKey                xmlns
="http://www.w3.org/2001/04/xmlenc#">
加密MachineKey                
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
加密MachineKey                加密MachineKey加密MachineKey加密MachineKey加密MachineKey加密MachineKey加密MachineKey加密MachineKey
加密MachineKey            
</EncryptedData>
加密MachineKey        
</machineKey>
这样的配置,你只要把这段配置复制到各个需要SSO的站点的Web.config就可以了,系统在运行过程中会自动进行解密
如果想变回原来的明文显示可以执行解密的相反过程就行
注意:其中的OpenWebConfiguration("/");表示打开站点根目录下的web.config,其它情况可参考如下:
Using the Management API

相关文章:

  • 2021-10-10
  • 2022-03-03
  • 2022-12-23
  • 2022-12-23
  • 2021-09-30
  • 2022-12-23
  • 2021-06-10
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-18
  • 2022-12-23
  • 2021-07-30
  • 2021-07-16
相关资源
相似解决方案