Ode*_*ded 25 .net encryption connection-string app-config
是否有一个实用程序将以与文件一样的方式加密文件中的命名配置部分(或仅connectionStrings
部分)?app.config
aspnet_regiis
web.config
我知道这可以在代码中完成 - 那里有代码示例,但我希望避免为此编写应用程序.
RBZ*_*RBZ 18
您可以尝试以下方法:
https://magenic.com/thinking/encrypting-configuration-sections-in-net
简而言之 - 将app.config
文件重命名为web.config
- 架构是相同的,因此aspnet_regiis
有效.app.config
完成后重命名.
老问题,但这是微软的方式:
.NET 2.0:http: //msdn.microsoft.com/en-us/library/89211k9b(v = vs.80).aspx
.NET 3.5:http://msdn.microsoft.com/en-us/library/ms254494 (v = vs.90).aspx("使用受保护的配置加密配置文件部分"一节)
在app.config文件上切换加密:
static void ToggleConfigEncryption(string exeConfigName)
{
// Takes the executable file name without the
// .config extension.
try
{
// Open the configuration file and retrieve
// the connectionStrings section.
Configuration config = ConfigurationManager.
OpenExeConfiguration(exeConfigName);
ConnectionStringsSection section =
config.GetSection("connectionStrings")
as ConnectionStringsSection;
if (section.SectionInformation.IsProtected)
{
// Remove encryption.
section.SectionInformation.UnprotectSection();
}
else
{
// Encrypt the section.
section.SectionInformation.ProtectSection(
"DataProtectionConfigurationProvider");
}
// Save the current configuration.
config.Save();
Console.WriteLine("Protected={0}",
section.SectionInformation.IsProtected);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Run Code Online (Sandbox Code Playgroud)
编译此控制台应用程序,并将配置文件拖到其上.它将吐出配置文件的副本,其连接字符串已加密.
请注意,您必须以将使用配置文件的同一用户进行加密.
using System;
using System.Configuration;
using System.IO;
namespace ConnectionStringEncryptor
{
class Program
{
static void Main(string[] args)
{
if (args.Length == 0)
{
throw new ArgumentException("Please supply a config file to encrypt");
}
string originalConfigFilePath = args[0];
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", originalConfigFilePath);
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConnectionStringsSection connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
connectionStringsSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
config.SaveAs(originalConfigFilePath + ".encrypted");
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
24114 次 |
最近记录: |