web.config 中的加密连接字符串出现错误
我的 web.config 中的连接字符串的加密功能有问题。
加密工作完美!但是,一旦启用加密,我就会丢失会话变量内容(会话变量为空异常)。
当我在 web.config 中停用连接字符串的加密时,一切恢复正常。
这是我的连接字符串加密代码:
#region Constructeur
static QueryManager()
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
ConnectionStringsSection section = config.GetSection("connectionStrings") as
ConnectionStringsSection;
if (section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
config.Save(ConfigurationSaveMode.Minimal);
}
if ((myConnectionString =
ConfigurationManager.ConnectionStrings["DBConnect"].ConnectionString) == null)
{
throw new ConfigurationErrorsException("Database server not configured");
}
section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
config.Save(ConfigurationSaveMode.Minimal);
}
#endregion
感谢您的帮助!
I have a problem with the encryption feature of my connection string in web.config.
The encryption works perfectly ! But as soon as the encryption is enabled I lose my Session variable content (Null exception on session variable).
When I deactivate the encryption of my connection string in the web.config, everything return to normal.
Here is my code for the encryption of the connection string :
#region Constructeur
static QueryManager()
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
ConnectionStringsSection section = config.GetSection("connectionStrings") as
ConnectionStringsSection;
if (section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
config.Save(ConfigurationSaveMode.Minimal);
}
if ((myConnectionString =
ConfigurationManager.ConnectionStrings["DBConnect"].ConnectionString) == null)
{
throw new ConfigurationErrorsException("Database server not configured");
}
section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
config.Save(ConfigurationSaveMode.Minimal);
}
#endregion
Thanks a million for your help !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该错误来自于设计错误。
解决方案如下:
然后:
这样,连接字符串就会在内存中解密并使用,而不会产生任何问题,并且避免了对 web.config 的许多无用的读写。
The error comes from a design error.
Here is the solution :
Then :
That way, the connection string is decrypted in memory and used without creating any problems and it avoid many useless read and write to the web.config.