如何加密web.config中的一个条目

Sta*_*tan 40 asp.net encryption web-config

ASP.NET 4

我在我的Web场上的web.config中对连接字符串使用了RSA密钥加密.但是,还有一个我要加密的自定义密码条目.如何使用RSA密钥对其进行加密,而不对其余配置进行加密.请指教,谢谢.

例:

  <appSettings>
        ...
    <add key="Host" value="www.foo.com" />
    <add key="Token" value="qwerqwre" />
    <add key="AccountId" value="123" />
    <add key="DepartmentId" value="456" />
    <add key="Password" value="asdfasdf" />
    <add key="SessionEmail" value="foo@foo.com" />
    <add key="DefaultFolder" value="789" />
  </appSettings>
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 61

您可以将密码放入单独的部分并仅加密此部分.例如:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="secureAppSettings" type="System.Configuration.NameValueSectionHandler, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    </configSections>

    <appSettings>
        <add key="Host" value="www.foo.com" />
        <add key="Token" value="qwerqwre" />
        <add key="AccountId" value="123" />
        <add key="DepartmentId" value="456" />
        <add key="SessionEmail" value="foo@foo.com" />
        <add key="DefaultFolder" value="789" />  
    </appSettings>

    <secureAppSettings>
        <add key="Password" value="asdfasdf" />
    </secureAppSettings>  
</configuration>
Run Code Online (Sandbox Code Playgroud)

然后(请注意我在我的示例中使用DPAPI,因此调整RSA的提供程序):

aspnet_regiis -pef secureAppSettings . -prov DataProtectionConfigurationProvider
Run Code Online (Sandbox Code Playgroud)

加密后,文件将如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="secureAppSettings" type="System.Configuration.NameValueSectionHandler, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    </configSections>

    <appSettings>
        <add key="Host" value="www.foo.com" />
        <add key="Token" value="qwerqwre" />
        <add key="AccountId" value="123" />
        <add key="DepartmentId" value="456" />
        <add key="SessionEmail" value="foo@foo.com" />
        <add key="DefaultFolder" value="789" />  
    </appSettings>

    <secureAppSettings configProtectionProvider="DataProtectionConfigurationProvider">
        <EncryptedData>
            <CipherData>
                <CipherValue>AQAAANCMnd.......</CipherValue>
            </CipherData>
        </EncryptedData>
    </secureAppSettings>  
</configuration>
Run Code Online (Sandbox Code Playgroud)

文件加密后,您在应用程序中访问这些设置的方式仍然相同且完全透明:

var host = ConfigurationManager.AppSettings["Host"];
var password = ConfigurationManager.AppSettings["Password"];
Run Code Online (Sandbox Code Playgroud)

  • 尝试使用以下命令:var section = ConfigurationManager.GetSection("secureAppSettings")作为NameValueCollection; var configPwd = section ["LoginPassword"]; (11认同)
  • @Stan,它使用`NameValueSectionHandler`. (3认同)
  • 我无法使用"ConfigurationManager.AppSettings ["Password"]从"secureAppSettings"部分检索"密码"的值;" 在.net 3.5 asp.net网站上. (2认同)

小智 12

在c#和.Net 4.5中,我不得不使用它来读取加密设置:

string password = ((System.Collections.Specialized.NameValueCollection)ConfigurationManager.GetSection("secureAppSettings"))["Password"].ToString();
Run Code Online (Sandbox Code Playgroud)

但否则就是一种享受.


Ode*_*ded 7

您无法加密单个条目 - 基础结构仅允许加密整个配置节.

一种选择是将条目放在其自己的配置部分并加密.