保护(加密)web.config文件中的密码(asp.net)

时间:2010-12-24 09:50:16

标签: asp.net

 <system.net>
  <mailSettings>
   <smtp from="email@domain.com" deliveryMethod="Network">
    <network clientDomain="www.domain.com" host="smtp.live.com" defaultCredentials="false" port="25" userName=" email@domain.com " password="password" enableSsl="true" />
   </smtp>
  </mailSettings>
 </system.net>

这是我需要加密密码的情况。我在网上搜索和搜索了很多,但我无法再加密了。

任何人都可以帮助我以简单但安全的方式做到这一点。

3 个答案:

答案 0 :(得分:6)

我在博客上写了一篇关于这个的文章:http://pvlerick.github.io/2009/03/encrypt-appconfig-section-using-powershell-as-a-post-build-event

我的想法是您希望在IDE中清除密码,但在输出文件夹的web.config / app.config中加密。

脚本是

param(
  [String] $appPath = $(throw "Application exe file path is mandatory"),
  [String] $sectionName = $(throw "Configuration section is mandatory"),
  [String] $dataProtectionProvider = "DataProtectionConfigurationProvider"
)

#The System.Configuration assembly must be loaded
$configurationAssembly = "System.Configuration, Version=2.0.0.0, Culture=Neutral, PublicKeyToken=b03f5f7f11d50a3a"
[void] [Reflection.Assembly]::Load($configurationAssembly)

Write-Host "Encrypting configuration section..."

$configuration = [System.Configuration.ConfigurationManager]::OpenExeConfiguration($appPath)
$section = $configuration.GetSection($sectionName)

if (-not $section.SectionInformation.IsProtected)
{
  $section.SectionInformation.ProtectSection($dataProtectionProvider);
  $section.SectionInformation.ForceSave = [System.Boolean]::True;
  $configuration.Save([System.Configuration.ConfigurationSaveMode]::Modified);
}

Write-Host "Succeeded!"

构建后命令是

powershell "& ""C:\Documents and Settings\VlericP\My Documents\WindowsPowerShell\EncryptAppConfigSection.ps1""" '$(TargetPath)' 'connectionStrings'

答案 1 :(得分:3)

这是加密和解密连接字符串的另一种方法,如果你使用的是vs2010,那么以管理员身份运行打开vs2010

string provider = "RSAProtectedConfigurationProvider"; 


string section = "connectionStrings";  

protected void btnEncrypt_Click(object sender, EventArgs e)  

{ 

   Configuration confg = 
   WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); 

   ConfigurationSection configSect = confg.GetSection(section); 

   if (configSect != null) 

   { 
      configSect.SectionInformation.ProtectSection(provider); 
      confg.Save(); 

   } 

} 
protected void btnDecrypt_Click(object sender, EventArgs e) 
{ 
    Configuration config = 
        WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); 
    ConfigurationSection configSect = config.GetSection(section); 
    if (configSect.SectionInformation.IsProtected) 
    { 
        configSect.SectionInformation.UnprotectSection(); 
        config.Save(); 
    } 
} 

答案 2 :(得分:1)

这是ASP.NET论坛上的一个主题,它正在进行一些头脑风暴并提供一些可能的解决方案:

How to encrypt the SMTP Node in web.config