Using ASPNet_Regiis to encrypt custom configuration section - can you do it?

前端 未结 5 2049
南旧
南旧 2020-12-23 17:18

I have a web application with a custom configuration section. That section contains information I\'ld like to encrypt (was hoping to use ASPNet_RegIIS rather than do it mys

5条回答
  •  悲&欢浪女
    2020-12-23 17:32

    For the record, I ended up with a little maintenance page to do this for me.

    var currentConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/");
    // Unprotect
    ConfigurationSection section = currentConfig.GetSection("MyCustomSection");
    if (section.SectionInformation.IsProtected)
    {
       section.SectionInformation.UnprotectSection();
       currentConfig.Save();
    }
    
    // Protect
    if (!section.SectionInformation.IsProtected)
    {
         section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
         currentConfig.Save();
    }
    

    Caveats: Your process will need write access to the config files being modified. You'll want some way to authorize who can run this. You'll generally restart the website when you Save.

提交回复
热议问题