• 沙里软件

  • ShaliSoft.com [手机站]   办公桌收纳抽屉
  • 首页
  • 博文
  • 演示
  • 管理
  • .net2.0 Webconfig中连接串的加密

    网络   2013/6/11 19:47:09

    【导读】ASP.NET 2.0 允许用户对配置文件的单个节进行加密本文。通过示例,演示如何以编程方式对配置节进行加密,配置API如何自动处理加密的节。


    ASP.NET 2.0 现在允许您对配置文件的单个节进行加密,这样,几乎不可能使用文本编辑器来读取这些配置节。

    ASP.NET 包括两个内置的受保护配置提供程序:RSA和DPAPI DPAPI提供程序使用特定于计算机的密钥,因此您必须在每台计算机上实际加密配置设置。默认使用的RSA提供程序允许您选择创建RSA密钥并将其安装在其他计算机上,这样您就可以在这些计算机之间复制相同的配置文件。此外,您还可以安装其他受保护配置提供程序供系统使用。

    调用配置管理API可透明地使用加密的节,因为该API自动处理加密和解密。若要通过编程方式将配置节设置为加密的,可获取ConfigurationSection.SectionInformation属性,然后传入您选择的保护提供程序调用ProtectSection方法。若要使用默认提供程序,可以传入null或空字符串。UnprotectSection方法禁用配置节的加密。

    下面的示例演示如何以编程方式对配置节进行加密,配置API如何自动处理加密的节。


    <%@ Import Namespace="System.Configuration" %>
    <%@ Import Namespace="System.Web.Configuration" %>
    <%@ Import Namespace="System.Xml" %>
    <script runat="server" language="C#">
    public void Page_Load(object source, EventArgs e)
    ...{
    if (!IsPostBack) ...{
    UpdateUI();
    }
    }
    void ProtectButton_OnClick(Object source, EventArgs e)
    ...{
    String path = Request.CurrentExecutionFilePath;
    path = path.Substring(0, path.LastIndexOf(’/’));
    // Get configuration.
    Configuration config = WebConfigurationManager.OpenWebConfiguration(path);
    ConfigurationSection appSettings = config.GetSection("appSettings");
    if (appSettings.SectionInformation.IsProtected)
    ...{
    appSettings.SectionInformation.UnprotectSection();
    }
    else
    ...{
    appSettings.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
    }
    try
    ...{
    config.Save();
    UpdateUI();
    }
    catch (Exception ex)
    ...{
    Response.Write("In order to modify configuration settings, the ASP.NET process account
    (either the local ASPNET or Network Service account, by default) ");
    Response.Write("must have write permission granted for the Web.config file
    in the sample directory");
    }
    }
    void UpdateUI()
    ...{
    String path = Request.CurrentExecutionFilePath;
    path = path.Substring(0, path.LastIndexOf(’/’));
    // Get configuration.
    Configuration config = WebConfigurationManager.OpenWebConfiguration(path);
    // Show XML for app settings.
    ConfigurationSection appSettings = config.GetSection("appSettings");
    // Set protect button appropriately.
    if (appSettings.SectionInformation.IsProtected)
    ...{
    Encrypted.Text = "Yes";
    ProtectButton.Text = "Unprotect";
    }
    else
    ...{
    Encrypted.Text = "No";
    ProtectButton.Text = "Protect";
    }
    // Show XML for app settings.
    AppSettingsXml.Text = " " + Server.HtmlEncode(appSettings.SectionInformation.GetRawXml());
    // Load XML directly from config file, to show encrypted XML.
    String configPath = Server.MapPath("web.config");
    XmlDocument doc = new XmlDocument();
    doc.PreserveWhitespace = true;
    doc.Load(configPath);
    XmlNode appSettingsXml = doc.SelectSingleNode("configuration/appSettings");
    AppSettingsEncrypted.Text = " " + Server.HtmlEncode(appSettingsXml.OuterXml);
    }
    </script>
    <html>
    <head>
    <title>Encrypted Configuration Sections</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <h2>Encrypted:<asp:Label runat="server" id="Encrypted" /></h2>
    <asp:Button runat="server" id="ProtectButton" OnClick="ProtectButton_OnClick" />
    <h2>Current XML (decrypted):</h2>
    <pre>
    <asp:Label runat="server" ID="AppSettingsXml" />
    </pre>
    <h2>Encrypted contents:</h2>
    <pre>
    <asp:Label runat="server" ID="AppSettingsEncrypted" />
    </pre>
    </div>
    </form>
    </body>
    </html>



    对应配置文件如下:



    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    <configProtectedData />
    <appSettings>
    <add key="currencyService" value="http://www.microsoft.com/services/currencyService.asmx" />
    <add key="creditCardValidationService" value="http://www.microsoft.com/services/cc.asmx" />
    </appSettings>
    </configuration>


    阅读(50746) 分享(0)

    上一篇: 利用XMLHTTP实现表单的提交以及cookies或session的发送
    下一篇: asp.net数据格式设置表达式

  • 精彩推荐

    ◆ 安装完office后 在组件服务里DCOM配置中找不到
    ◆ 微信清缓存工具,微信怎么清理缓存?
    ◆ 用回溯法解决子集和问题【C#版本】
    ◆ 实测什么物体会影响WIFI信号
    ◆ 利用UC微信分享接口进行WEB微信分享
    ◆ ASP.NET之GridView Eval() 中数据格式化或格式化数据
    ◆ css常用hack语法
    ◆ 面向对象的缺点,你了解了吗
    ◆ 我国首台可人脸识别ATM机发布 不刷脸不能取钱
    ◆ 2G网络要关闭了吗?你还不打算换4G手机?
  • 用心做事 不能唯利是图

    • 吊儿
    • 用QQ联系我17905772
  • 搜索


  • 最新文章

    • 导出Excel 格式 mso-number-format
    • 服务器iis支持tls1.2,windows server 2008 r2 中IIS启用TLS 1.2(安装SSL后用TLS 1.2)
    • MySQL配置优化
    • EditPlus 添加文件比较工具winmerge
    • 滚动悬浮固定JS特效

  • 热门文章

    • php sso单点登录实现代码
    • 中国菜刀(China chopper) 最新黑客工具
    • redis.conf中文版(基于2.4)
    • 搜索引擎名单大全
    • php图片上传类,支持加水印,生成略缩图

  • 最新图库


  • 最新评论


  • 友情链接

  • 沙里软件

  • 最近访客

    Powered by ShaliSoft.com 豫ICP备13008529号

    免责声明:本站部分内容来源于互联网,转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,不为其版权负责,也不构成任何其他建议。如果发现侵犯版权,联系QQ17905772进行删除。