защитить (зашифровать) пароль в файле web.config (asp.net) - PullRequest
7 голосов
/ 24 декабря 2010
 <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 ]

6 голосов
/ 24 декабря 2010

Я написал статью об этом в своем блоге: 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'
3 голосов
/ 08 мая 2012

Это еще один способ зашифровать и дешифровать строку соединения. Проверьте его, если вы используете 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(); 
    } 
} 
1 голос
/ 24 декабря 2010

Вот ветка на форумах ASP.NET, в которой проводится мозговой штурм и предлагается несколько возможных решений:

Как зашифровать узел SMTP в web.config

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...