本文介绍了C#创建Web.config文件编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写和安装ASP.NET应用程序,和我有几个数据字段(连接字符串,SMTP服务器等),我想要的安装程序来构建从用户的输入,加密和存储在Web.config文件。我遇到的问题是,无论WebConfigurationManager和ConfigurationManager中都设计工作,关闭现有的配置文件。我怎样才能构建一个新的配置文件,并保存之前对其进行加密?

I'm writing and installer for an ASP.NET application, and I have a few data fields, (connection strings, smtp servers, etc), that I want the installer to build from user input, encrypt and store in the Web.config file. The issue I am having is that both WebConfigurationManager and ConfigurationManager are both designed to work off existing configuration files. How can I construct a new configuration file, and encrypt it before saving it?

推荐答案

我会建议先从不包含你想要的字段,然后使用一个基本配置文件的XDocument WebConfigurationManger 添加配置信息,并对其进行加密。

I would recommend starting with a base configuration file that does not contain fields you want, then using XDocument or WebConfigurationManger add the configuration information and encrypt it.

来源:加密和在你的web.config文件解密敏感数据使用受保护的配置 - 第四部分

code柜面含量下降:

Code incase the content goes down:

private void EncryptConfig()
{
    // Open the Web.config file.
    Configuration config =
        WebConfigurationManager.OpenWebConfiguration("~");
    // Get the connectionStrings section.
    ConnectionStringsSection section =
    config.GetSection("connectionStrings") as ConnectionStringsSection;

    // Toggle encryption.
    if (section.SectionInformation.IsProtected)
    {
        section.SectionInformation.UnprotectSection();
    }
    else
    {
    if (!section.SectionInformation.IsLocked)
    {
        section.SectionInformation
               .ProtectSection("RsaProtectedConfigurationProvider");

        section.SectionInformation.ForceSave = true;

        //Save changes to the Web.config file.
        config.Save(ConfigurationSaveMode.Full);           }
    }
}

这篇关于C#创建Web.config文件编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 09:05