Web.Config - Criptografar ConnectionString

<< Click to Display Table of Contents >>

Navigation:  ASP.NET > Dicas avançadas >

Web.Config - Criptografar ConnectionString

Previous pageReturn to chapter overviewNext page

Web.Config - ANTES - Normal

 

<?xml version="1.0"?>

 

<configuration>

 <system.web>

   <compilation debug="true" targetFramework="4.0" />

 </system.web>

 

 <connectionStrings>

   <add name="conexao"

        connectionString="User ID=postgres;Password=.123456#;Host=192.168.10.105;Port=5432;Database=empresa"/>

 </connectionStrings>

 

</configuration>

 

Método para criptografar o Web.Config

 

using System.Configuration;

using System.Web.Configuration;

 

{...}

 

 Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");

 ConfigurationSection configurationSection = configuration.GetSection("connectionStrings");

 configurationSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");

 configurationSection.SectionInformation.ForceSave = true;

 configuration.Save(ConfigurationSaveMode.Full);

 

Execute este método 1 vez.

 

Web.Config - DEPOIS

 

<?xml version="1.0"?>

 

<configuration>

 <system.web>

   <compilation debug="true" targetFramework="4.0" />

 </system.web>

 

 <connectionStrings configProtectionProvider="DataProtectionConfigurationProvider">

   <EncryptedData>

     <CipherData>

       <CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAw4zipiF2OkeN/7x8QxGjpwQAAAACAAAAAAAQZgAAAAEAACAAAAC+nYboXd38zzBkMjppGA2VrwiodeLswaTqE/ZoQNJWpgAAAAAOgAAAAAIAACAAAABAxc0DXU9da8ei28b4REh7Os6gcHlRe0l8khCxjrOfmNADAACZvg81fKF2JVnV4GTk13JkuszvFF8gTCzdcNM2yARw+72jMZXXiHr8AOzM3UrVtRXtUPqtpXSdQb4KFoWLIUSt0OvmGa7VBb2+4vL8YjKu9BnEjbeP22cI4wjFxSkeU9o6FS4LPFD4aOqimbtHCK3bOFgiA1QT75N1aPa1oK1EY5udgm4+Pow/e0/iaDQ40iRRHR2d8Qm1flKuKBppq4MM3Pu4ubJ03pADRkPYxpXtd/N2Ys4aPrR/ni7Dfzc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75J4iTNIAEDg55uI4/RluAZ1utay+Wxc7ld+AavEac=</CipherValue>

     </CipherData>

   </EncryptedData>

 </connectionStrings>

 

</configuration>

 

Default.aspx.cs

 

Lendo a conexão criptografada

 

 string s = ConfigurationManager.ConnectionStrings["conexao"].ConnectionString;

 Response.Write(s);