ASP.NET MVC系列:web.config中ConnectionString aspnet_iis加密與AppSettings獨立檔案

libingql發表於2018-05-21

1. web.config中ConnectionString aspnet_iis加密

  web.config路徑:E:\Projects\Libing.Web\web.config

<connectionStrings>
  <add name="DBConnectionString" connectionString="Data Source=.;User ID=sa;Password=1;Initial Catalog=DB;Persist Security Info=True;" />
</connectionStrings>

  aspnet_iis加密connectionString:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis -pef "connectionStrings" "E:\Projects\Libing.Web"
C:\Users\libi>C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis -pef "connectionStrings" "E:\Projects\Libing.Web"
Microsoft (R) ASP.NET RegIIS 版本 4.0.30319.0
用於在本地計算機上安裝和解除安裝 ASP.NET 的管理實用工具。
版權所有(C) Microsoft Corporation。保留所有權利。
正在加密配置節...
成功!
<connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
  <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
    xmlns="http://www.w3.org/2001/04/xmlenc#">
    <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
    <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
      <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
        <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
        <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
          <KeyName>Rsa Key</KeyName>
        </KeyInfo>
        <CipherData>
          <CipherValue>QHf/c44+ATukJ2ZPqULQPs4Llu7JcUnvgekiNl/ABT+be5W38eUJO0zgM9oN6MvoIsd8BDHAPOXBsr2Sqj/4hA6n2KKUrPlb9tMzyafFL9nUmrs0oCJSLd+mCI2ldrhSc1uTV0XBP35bvLm2VPXRxAXDhvvxq8qnX54Oosq0QB8DGbwdT64hb2xzrWiWXoyw7+HfvK8jLZ9XFhSHXk7/OQiMZfyRp8Iqkt3+KeycjWhm0aU2HPFdJD5vcnW+fWCtbP3wwO9JrD5OW0OI9zWEV40qpaKzLkQLNUCNLRsJI6K78L0xBC25CEnC3fMmRYdHjSwXmAZYRzZu+0jt+f+ZiQ==</CipherValue>
        </CipherData>
      </EncryptedKey>
    </KeyInfo>
    <CipherData>
      <CipherValue>KMnJpDpH3+qpTmE0bqQSm72zNhopKQqmPdrxZoj3p4CV7ZxQTZEZhZ7EH2KFHjIy6WufonggfqO6m0jGjTO7NgqkwRZL/fo+Od9gR7xnIc8mIedFnJNEXWb4SkwZ+Lb5U1LsmsIXYacr6jA0ZbQVyWjcK+jH5yO2gWTYQdf2MR3AVwMXzUiU2KH40x3OcZMVd4zrjIS71SnqJ/JC73X1WlmoeaRpzlZfKdx+IySt37qeacHOUtHObUG4VDpfv3JV</CipherValue>
    </CipherData>
  </EncryptedData>
</connectionStrings>

  aspnet_iis解密connectionString:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis -pdf "connectionStrings" "E:\Projects\Libing.Web"
C:\Users\libi>C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis -pdf "connectionStrings" "E:\Projects\Libing.Web"
Microsoft (R) ASP.NET RegIIS 版本 4.0.30319.0
用於在本地計算機上安裝和解除安裝 ASP.NET 的管理實用工具。
版權所有(C) Microsoft Corporation。保留所有權利。
正在解密配置節...
成功!

2. web.config中AppSettings獨立檔案

  Configs/AppSetting.config:(AppSetting.config存放Configs資料夾中)

<?xml version="1.0" encoding="utf-8"?>
<appSettings>
  <add key="webpages:Version" value="3.0.0.0"/>
  <add key="webpages:Enabled" value="false"/>
  <add key="ClientValidationEnabled" value="true"/>
  <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
  
  <add key="AppVersion" value="1.0"/>
</appSettings>

  web.config:

<appSettings configSource="Configs\AppSettings.config"></appSettings>

  讀取配置:

System.Configuration.ConfigurationManager.AppSettings["AppVersion"]

相關文章