<machineKey validationKey="282487E295028E59B8F411ACB689CCD6F39DDD21E6055A3EE480424315994760ADF 21B580D8587DB675FA02F79167413044E25309CCCDB647174D5B3D0DD9141" decryptionKey="8B6697227CBCA902B1A0925D40FAA00B353F2DF4359D2099" validation="SHA1"/>
这个样例代码并没有被加密,并且它不会被发布到上。因为处于考虑,发布到服务器的<machineKey>的加密是非常重要的。你可以在清单2中看到加密后的<machineKey>。
清单 2: web.config 中加密后的machineKey
<machineKeyconfigProtectionProvider="RsaProtectedConfigurationProvider"> <EncryptedDataType="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#"> <EncryptionMethodAlgorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" /> <KeyInfoxmlns="http://www.w3.org/2000/09/xmldsig#"> <EncryptedKeyxmlns="http://www.w3.org/2001/04/xmlenc#"> <EncryptionMethodAlgorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" /> <KeyInfoxmlns="http://www.w3.org/2000/09/xmldsig#"> <KeyName>Rsa Key</KeyName> </KeyInfo> <CipherData> <CipherValue> lm3mfPX/94Zm3HgdbsmKiIxbrWM14t3/ugxs40BFOAHbIaCtwQ3gVQusFtOFVUoNVny01kgBCeh10rVEId djNZ/8luBNoCbHm8OLjgPLHVrT+G0c/LRpESJk2ni/Jy2sWKXlgejgSQ1W5NE53GZtG3s9hu+nk4OWxntS 6z3v7AM= </CipherValue> </CipherData> </EncryptedKey> </KeyInfo> <CipherData> <CipherValue> BCEGUV/dh1Imbcm5vn0Kn8NrD+EX+KemenR7x+VekwT1ZO6y5+jRyF4RDWMJCfJ1jHC36+MAfCdHuXN0rP B6hu5YUtX9VA5q5N0NGrs9AIpG+0ihuuS3HDzQe3P6nlI30m1h0pmL1yJBovY0i6fbCA6++GT2MdwCLERk +PVWmoq7p1q97n5pNzNqhVKCX45lhS5ySVS+MjJXVeTrcatftpvaUcjLsNcL2kMerzf5w/SU3AbLEuY04w dgYWX5tWzxqeUcghdlWLD0tQi8qyyfVfzXPYozR5sspWHdgqmAycrACHN2dcONWPjT4BanRWb1ouKuP8K+ 0CEFE/Hj2ChpYw== </CipherValue> </CipherData> </EncryptedData> </machineKey>你可以通过Configuration、SectionInformation两个类来加密你的配置文件。为了加密和解密你的<machineKey>让我们来写一些代码吧。SectionInformation类有一个方法ProtectSection(),可以得到一个描绘Protection Provider的字符串比如"RSAProctedConfigurationProvider",并且加密这个配置节。这里还有一个Boolean类型的属性ForceSave,当需要配置类的save方法保存配置文件时需要将它设置为true。这里有"Encryption.aspx"页面的代码,页面中包含有两个按钮来加密和解密配置文件。
protected void btnEncrypt_Click(object sender, EventArgs e)
{
try
{
Configuration config = WebConfigurationManager.OpenWebConfiguration(
"/Aspalliance1 ");
ConfigurationSection machineKeySection = config.GetSection(
"system.web/machineKey");
machineKeySection.SectionInformation.ProtectSection(
"RSAProtectedConfigurationProvider");
machineKeySection.SectionInformation.ForceSave = true;
config.Save();
Response.Write("<h2 style='color:red'>Encryption Succeed</h2>");
}
catch (Exception ex)
{
Response.Write("<h2 style='color:red'>Error while encrypting</h2><br/>");
Response.Write(ex.Message);
}
}
清单 4: web配置文件的解密代码
protected void btnDecrypt_Click(object sender, EventArgs e)
{
try
{
Configuration config = WebConfigurationManager.OpenWebConfiguration(
"/Aspalliance1 ");
ConfigurationSection machineKeySection = config.GetSection(
"system.web/machineKey");
machineKeySection.SectionInformation.UnprotectSection();
machineKeySection.SectionInformation.ForceSave = true;
config.Save();
Response.Write("<h2 style='color:red'>Decryption Succeed</h2>");
}
catch (Exception ex)
{
Response.Write("<h2 style='color:red'>Error while decrypting</h2><br/>");
Response.Write(ex.Message);
}
}
现在你必须在这个站点中设置相同的配置。首先你需要更改你的窗体验证部分的loginUrl,这个窗体将被用来将匿名用户跳转到”Login.aspx”页。只是,现在它将把用户重定向到Aspalliance1站点中的”Login.aspx”页。
清单 5: 设置 web.config中的验证节
<authentication mode="Forms"> <forms loginUrl="http://localhost/Aspalliance1/login.aspx"name=".ASPXAUTH"/> </authentication>
如果你想实现跨程序登录你的好多站点时,最重要的一点就是你必须把你的两个或更多的站点配置为相同的<machineKey>。所以我只需要拷贝并粘贴Aspalliance1 站点中的<machineKey>配置节到Aspalliance2站点。现在都已经准备好了,你可以测试你的站点了。
清单 6: 设置web.config 中的 machineKey
<machineKey validationKey="282487E295028E59B8F411ACB689CCD6F39DDD21E6055A3EE480424315994760ADF 21B580D8587DB675FA02F79167413044E25309CCCDB647174D5B3D0DD9141" decryptionKey="8B6697227CBCA902B1A0925D40FAA00B353F2DF4359D2099" validation="SHA1"/>
测试这个站点的话,可以使用用户名:Admin密码:123456&来登录。
这个附件中有一个VS 2005项目,其中包含有两个站点:aspalliance1 and aspalliance2.
要安装这个实例的话,你需要创建两个IIS虚拟目录命名为:aspalliance1 和 aspalliance2,并将地址指向相应的文件夹。你也可以通过Visual Studio 2005打开站点。
当用户要交叉访问你的多个站点时,他必须重复登陆实在是麻烦。所以,如果只让用户登录一次,那会是非常棒的。实现这些,你只需要给你的"web.config" 文件增加具有相同值的<machineKey>配置。并且处于安全考虑,我建议你加密这个配置节。这个加密方法在SectionInformation类中通过ProtectSection()方法被重写了。
文档说明:
相关文档
返回首页 | 关于本站 | | 友情链接 | 广告服务 | 意见建议 | 访客留言 | 本站论坛
Copyright© 2001-2006 ProgramBBS.com All Rights Reserved 版权所有©编程论坛
Email: 吉ICP备05009985号
感谢长春订餐网友情支持