保护 Web 应用程序之外的 web.config 部分
我想创建一个 msbuild 任务来加密 web.configs 的某些部分。以下代码在 weapplication 中运行良好。作为 msbuild 运行代码会导致错误,指出它无法创建配置文件。
System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.GetSection(sectionName);
if (section != null && !section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection(provider);
config.Save();
}
我找不到任何可以完成正确工作的类。有人有想法吗?
I want to create a msbuild task which encrypts certain sections of my web.configs. The following code works great inside a weapplication. Running the code as an msbuild causes an error saying it cannot create the config file..
System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.GetSection(sectionName);
if (section != null && !section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection(provider);
config.Save();
}
I couldn't find any classes which do the right job. Ideas anyone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该创建自己的自定义 MSBuild 任务。
下面的代码是一个自定义任务。
我已经使我的应用程序(winforms)具有功能,但我标记了您可以更改的基于网络的行。
我创建了一个带有 2 个子类的抽象类来处理加密和解密。
干杯!
这也会有帮助:
http://www.codeproject.com/KB/dotnet/EncryptingTheAppConfig.aspx
http://www.beansoftware.com/ASP.NET-Tutorials/Encrypting-Connection-String.aspx
但是封装到 MSBuild 任务中是我的贡献。
上面的第二个 URL 还提到了命令行方法:
Here is that引用的材料(部分引用):::
Encryption/Decryption using aspnet_regiis.exe command line tool
您还可以使用以下命令加密和解密 Web.config 文件中的部分aspnet_regiis.exe 命令行工具,可以在\Microsoft.Net\Framework\version 目录中找到。要使用此命令行工具使用 DPAPI 计算机密钥加密 Web.config 的一部分,请使用以下命令。
aspnet_regiis.exe -pe "connectionStrings" -app "/YourWebSiteName" –prov "DataProtectionConfigurationProvider"
要使用此工具解密connectionStrings部分,您可以在aspnet_iisreg.exe工具中指定以下命令。
aspnet_regiis.exe -pd“connectionStrings”-app“/YouWebSiteName”
You should create your own custom MSBuild task.
The below code is a custom task.
I've made mine application(winforms) capable, but I marked the lines you can change for web based.
I've created an abstract class with 2 subclasses to handle encrypt and decryption.
Cheers!
This will help as well:
http://www.codeproject.com/KB/dotnet/EncryptingTheAppConfig.aspx
http://www.beansoftware.com/ASP.NET-Tutorials/Encrypting-Connection-String.aspx
But the encapsulation into a MSBuild Task is my contribution.
The second URL above also mentions a command line method:
Here is that quoted material (partial quote that is):::
Encryption/Decryption using aspnet_regiis.exe command line tool
You can also encrypt and decrypt sections in the Web.config file using the aspnet_regiis.exe command-line tool, which can be found in the \Microsoft.Net\Framework\version directory. To encrypt a section of the Web.config using the DPAPI machine key with this command-line tool, use following command.
aspnet_regiis.exe -pe "connectionStrings" -app "/YourWebSiteName" –prov "DataProtectionConfigurationProvider"
To decrypt connectionStrings section using this tool, you can specify following command in aspnet_iisreg.exe tool.
aspnet_regiis.exe -pd "connectionStrings" -app "/YouWebSiteName"