[英]Encrypting connection string: “This operation does not apply at runtime”
I have a console application and it has app.config. 我有一个控制台应用程序,它有app.config。 When I run this code:
当我运行此代码时:
class Program
{
static void Main()
{
ConnectionStringsSection connSection = ConfigurationManager.GetSection("connectionStrings") as
ConnectionStringsSection;
if (connSection != null)
{
if (!connSection.SectionInformation.IsProtected)
connSection.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
else
connSection.SectionInformation.UnprotectSection();
}
Console.Read();
}
}
I get error: "This operation does not apply at runtime". 我收到错误:“此操作在运行时不适用”。 I also tried giving permissions to my app.config but no luck.
我也试过给我的app.config权限,但没有运气。
What can the issue? 有什么问题?
You can try the following: 您可以尝试以下方法:
static void Main()
{
// Get the current configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
ConnectionStringsSection connSection = config.GetSection("connectionStrings") as
ConnectionStringsSection;
if (connSection != null)
{
if (!connSection.SectionInformation.IsProtected)
connSection.SectionInformation.ProtectSection(null);
else
connSection.SectionInformation.UnprotectSection();
}
connSection.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);
Console.ReadKey();
}
I think you are supposed to use the OpenExeConfiguration
method in this scenario: 我认为你应该在这种情况下使用
OpenExeConfiguration
方法:
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(pathToExecutable);
ConnectionStringsSection connSection =
config .GetSection("connectionStrings") as ConnectionStringsSection;
the parameter pathToExecutable
should be the full path to the exe of your application, for example: "C:\\application\\bin\\myapp.exe" 参数
pathToExecutable
应该是应用程序的exe的完整路径,例如:“C:\\ application \\ bin \\ myapp.exe”
You are not supposed to encrypt sections at runtime, you encrypt them before runtime with the aspnet_setreg.exe tool. 您不应该在运行时加密部分,使用aspnet_setreg.exe工具在运行时加密它们。 More info here.
更多信息在这里。
ASP.NET then reads the encrypted sections at runtime transparently. 然后ASP.NET透明地在运行时读取加密的部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.