關於.net2.0中Webconfig中連接串的加密

ASP.NET 2.0 現在允許您對配置文件的單個節進行加密,這樣,幾乎不可能使用文本編輯器來讀取這些配置節。 ASP.NET 包括兩個內置的受保護配置提供程序:RSA 和 DPAPI DPAPI 提供程序使用特定於計算機的密鑰,因此您必須在每臺計算機上實際加密配置設置。默認使用的 RSA 提供程序允許您選擇創建 RSA 密鑰並將其安裝在其他計算機上,這樣您就可以在這些計算機之間複製相同的配置文件。此外,您還可以安裝其他受保護配置提供程序供系統使用。 調用配置管理 API 可透明地使用加密的節,因爲該 API 自動處理加密和解密。若要通過編程方式將配置節設置爲加密的,可獲取 ConfigurationSection.SectionInformation 屬性,然後傳入您選擇的保護提供程序調用 ProtectSection 方法。若要使用默認提供程序,可以傳入 null 或空字符串。UnprotectSection 方法禁用配置節的加密。 下面的示例演示如何以編程方式對配置節進行加密,配置 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>




郊野如圖:






 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章