Wednesday 18 September 2019

Encrypt and Decrypt web.config using asp.net

////////////////



 public static void EncryptConnString()
    {
        string msg="";
        Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
        ConfigurationSection section = config.GetSection("connectionStrings");
        if (!section.SectionInformation.IsProtected)
        {
            section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
            config.Save();
        }

        ///////////// or

        Configuration config1 = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
        ConfigurationSection configSection = config1.GetSection("connectionStrings");
        if (!configSection.SectionInformation.IsProtected)
        {
            configSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
            config1.Save();            
            msg="ConnectionStrings has been encryted successfully.";
        }
        else
        {
            msg="ConnectionStrings Already encryteded, this action has been cancled";
        }
    }


    public static void DecryptConnString()
    {
        string msg = "";

        Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
        ConfigurationSection section = config.GetSection("connectionStrings");
        if (section.SectionInformation.IsProtected)
        {
            section.SectionInformation.UnprotectSection();
            config.Save();
        }
        ///////////////// or

        Configuration config1 = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
        ConfigurationSection configSection = config1.GetSection("connectionStrings");
        if (configSection.SectionInformation.IsProtected)
        {
            section.SectionInformation.UnprotectSection();
            config.Save();
            msg = "ConnectionStrings has been Decrypt successfully.";
        }
        else
        {
            msg = "ConnectionStrings Already Decrypted, this action has been cancled";
        }
      
    }


/////////// Result



///////////--------Original 

     <connectionStrings>

     <add name="ConnDB" connectionString="Data Source=Abc;Initial Catalog= Main;Integrated Security=True;Connect Timeout=500" providerName="System.Data.SqlClient"/>
  </connectionStrings>



///////////--------Encrypted


 <connectionStrings configProtectionProvider="DataProtectionConfigurationProvider">

    <EncryptedData>
      <CipherData>
        <CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAYGhwDVq4A0K6/N1Iz7hkjwQAAAACAAAAAAAQZgAAAAEAACAAAAAQLUts2b81u6Qla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VF538Ca2VE4j66usInzQ1AuAyDe84PnefTGA98rtmFYQE8yiHU71iYGTdjtSPbOF71B8TiazNO4kgtcdHudOOLf4on0Jk0J0AcYtBMXIcKY0VfrDsIs9JkDD4Hsn0Jt+w9meC/akzZegCaF3oQpCSqLyg80xNL+npzFMuLjFobuF5s49pAAAAAVHx94/aTalWw5ZWqlzwxFMnV3Fo2ow7jCGBuo/qqomAxxmQS3zAhfREHlPGZuzKkcASqnuS+XIoFPKWUIePBTQ==</CipherValue>
      </CipherData>
    </EncryptedData>
  </connectionStrings>

No comments:

Post a Comment

Excel Sort values in ascending order using function TEXTJOIN

 Excel ::  Text ::  1,3,5,2,9,5,11 Result :: 1,2,3,5,5,9,11 Formula ::     TEXTJOIN ( ",",1,SORT(MID(SUBSTITUTE( A1 ,","...