开发者

Encryption in DATA access block

开发者 https://www.devze.com 2022-12-31 17:55 出处:网络
I am using enterprise library DATA access block in my application and now I want to encrypt the connection string and store it in the config file and consume it in my 开发者_高级运维application after

I am using enterprise library DATA access block in my application and now I want to encrypt the connection string and store it in the config file and consume it in my 开发者_高级运维application after decrypting the same. How can I do this?


You can encrypt sections of your web.config using DPAPI provider. Nothing else need to change in your application. you still keep reading appsettings and conn. strings as usual.

//call: ProtectSection("appSettings","DataProtectionConfigurationProvider");
private void ProtectSection(string sectionName, string provider)
{
    Configuration config =
        WebConfigurationManager.
            OpenWebConfiguration(Request.ApplicationPath);

    ConfigurationSection section = config.GetSection(sectionName);

    if (section != null && !section.SectionInformation.IsProtected)
    {
        section.SectionInformation.ProtectSection(provider);
        config.Save();
    }
}

//call: UnProtectSection("appSettings");
private void UnProtectSection(string sectionName)
{
    Configuration config =
        WebConfigurationManager.
            OpenWebConfiguration(Request.ApplicationPath);

    ConfigurationSection section = config.GetSection(sectionName);

    if (section != null && section.SectionInformation.IsProtected)
    {
        section.SectionInformation.UnprotectSection();
        config.Save();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号