[英]Encrypting connection string: “This operation does not apply at runtime”
我有一个控制台应用程序,它有app.config。 当我运行此代码时:
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();
}
}
我收到错误:“此操作在运行时不适用”。 我也试过给我的app.config权限,但没有运气。
有什么问题?
您可以尝试以下方法:
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();
}
我认为你应该在这种情况下使用OpenExeConfiguration
方法:
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(pathToExecutable);
ConnectionStringsSection connSection =
config .GetSection("connectionStrings") as ConnectionStringsSection;
参数pathToExecutable
应该是应用程序的exe的完整路径,例如:“C:\\ application \\ bin \\ myapp.exe”
您不应该在运行时加密部分,使用aspnet_setreg.exe工具在运行时加密它们。 更多信息在这里。
然后ASP.NET透明地在运行时读取加密的部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.