Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

protect (encrypt) password in the web.config file (asp.net)

Tags:

asp.net

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

This is the case where I need encryption for my password. I searched and googled much on the web but I can’t be able to encrypt anymore.

Can anyone help me do this in a simple but secure way.

like image 298
Muaz Khan Avatar asked Dec 24 '10 09:12

Muaz Khan


2 Answers

I wrote an article about that on my blog: http://pvlerick.github.io/2009/03/encrypt-appconfig-section-using-powershell-as-a-post-build-event

My idea was that you want the password to be clear in the IDE, but encrypted in the output folder's web.config/app.config.

The script is

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!"

The post-build command is

powershell "& ""C:\Documents and Settings\VlericP\My Documents\WindowsPowerShell\EncryptAppConfigSection.ps1""" '$(TargetPath)' 'connectionStrings'
like image 135
Philippe Avatar answered Sep 19 '22 02:09

Philippe


This is another way to encrypt and decrypt coonection string check it if you are using vs2010 then open vs2010 with run as administrator

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(); 
    } 
} 
like image 32
lucky One Avatar answered Sep 19 '22 02:09

lucky One