【发布时间】:2016-10-01 02:38:38
【问题描述】:
我可以使用 aspnet_regiis.exe 命令配置连接字符串加密。现在我创建了配置部分,在该部分中添加了自定义配置元素集合,这将存储连接信息的值。
namespace ExpressSnapSortCreation
{
/// <summary>
/// This Class hold the the Collection of Cofigration key
/// </summary>
internal class ServerReplicationsCollection : ConfigurationElementCollection
{
/// <summary>
/// This Will return the ConfigurationElement
/// </summary>
/// <returns>ConfigurationElement</returns>
protected override ConfigurationElement CreateNewElement()
{
return new ServerReplicationsElement();
}
/// <summary>
/// Get Element BY key
/// </summary>
/// <param name="element"></param>
/// <returns></returns>
protected override object GetElementKey(ConfigurationElement element)
{
return ((ServerReplicationsElement)element).Name;
}
/// <summary>
/// This is override on the Elements
/// </summary>
public class ServerReplicationsElement : ConfigurationElement
{
/// <summary>
/// Name of the Element
/// </summary>
[ConfigurationProperty("name", IsRequired = true)]
public string Name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
/// <summary>
/// Data base name
/// </summary>
[ConfigurationProperty("connectionString", IsRequired = true)]
public string ConnectionString
{
get { return (string)this["connectionString"]; }
set { this["connectionString"] = value; }
}
/// <summary>
/// Data base user name
/// </summary>
[ConfigurationProperty("providerName", IsRequired = true)]
public string ProviderName
{
get { return (string)this["providerName"]; }
set { this["providerName"] = value; }
}
/// <summary>
/// Display Order
/// </summary>
[ConfigurationProperty("order", IsRequired = false)]
public int Order
{
get { return (int)this["order"]; }
set { this["order"] = value; }
}
}
}
}
这是创建部分的代码
class ServerReplications : ConfigurationSection
{
/// <summary>
/// The name of this section in the app.config.
/// </summary>
public const string SectionName = "ReplicationConfigurationSection";
/// <summary>
/// Replication data base name
/// </summary>
private const string ReplicationCenterCollectionName = "ReplicationDataBases";
[ConfigurationProperty(ReplicationCenterCollectionName)]
[ConfigurationCollection(typeof(ServerReplicationsCollection), AddItemName = "add")]
public ServerReplicationsCollection ReplicationDataBases { get { return (ServerReplicationsCollection)base[ReplicationCenterCollectionName]; } }
}
这是我的应用配置文件。
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="ReplicationConfigurationSection"
type="ExpressSnapSortCreation.ServerReplications, ExpressSnapSortCreation" />
</configSections>
<ReplicationConfigurationSection>
<ReplicationDataBases>
<add name="ApplicationServices" connectionString="Data Source=PC-002\SQLEXPRESS2014;Initial Catalog=AML25;Persist Security Info=True;User ID=sa;Password=StItS!@#SeRvErPC-003" providerName="System.Data.SqlClient" order="1" />
<add name="ApplicationServices2" connectionString="Data Source=PC-004\SQLEXPRESS2014;Initial Catalog=AML26;Persist Security Info=True;User ID=sa;Password=StItS!@#SeRvErPC-002" providerName="System.Data.SqlClient" order="2" />
</ReplicationDataBases>
</ReplicationConfigurationSection>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
</startup>
</configuration>
在应用程序中,我们获取连接字符串的值。由于安全目的,我们无法在 App.config 值中显示数据。那么就需要对下面的Section进行加密
-
这是我使用的第一个命令
aspnet_regiis.exe -pef "ReplicationConfigurationSection" "C:\Users\mukesh.singh\Documents\Visual Studio 2015\Projects\AML\ExpressSnapSortCreation"
出现错误将文件名“app.config”转换为“Web.config”
创建配置节处理程序时出错 对于 ReplicationConfigurationSection:无法加载文件或程序集 'ExpressSnapSortCreation' 或其依赖项之一。系统 找不到指定的文件。 (C:\Users\mukesh.singh\Documents\Visual Studio 2015\Projects\AML\ExpressSnapSortCreation\bin\Debug\web.config 第 4 行)
无法加载文件或程序集“ExpressSnapSortCreation”或其中之一 它的依赖关系。该系统找不到指定的文件。失败!
- 更改后
无法从 程序集'System.Web,版本=4.0.0.0,文化=中性, PublicKeyToken=b03f5f7f11d50a3a'。
-
我也试过这个组合
aspnet_regiis.exe -pef "ExpressSnapSortCreation.ServerReplications/ExpressSnapSortCreations" "C:\Users\mukesh.singh\Documents\Visual Studio 2015\Projects\AML\ExpressSnapSortCreation
【问题讨论】:
-
您不能使用 aspnet_regiis.exe 加密 app.config 文件。您可以做的是通过一些代码对其进行加密。见stackoverflow.com/questions/2203578/…
-
感谢 dbugger 它为我工作。我正在尝试使用 cmd 加密数据现在我能够解决它。
-
@dbugger 如果我想对窗口服务 App.Config 进行加密,如何实现此代码。在服务中,我无法加密该部分。你能帮我解决这个问题吗?
-
问另一个问题——不过,它的工作原理是一样的。
-
我试了一下,但它不适合我。给出错误 System.Configuration.dll 中发生“System.Configuration.ConfigurationErrorsException”类型的未处理异常附加信息:为 ReplicationConfigurationSection 创建配置节处理程序时出错:无法加载文件或程序集“ExpressSnapSortCreation”或其中之一它的依赖关系。系统找不到指定的文件。
标签: asp.net encryption web-config app-config aspnet-regiis.exe