web.config 中的加密连接字符串出现错误

发布于 2024-12-10 23:38:22 字数 972 浏览 0 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枯寂 2024-12-17 23:38:22

该错误来自于设计错误。

解决方案如下:

  • 首先,必须在应用程序外部进行加密,以避免每次向数据库发出请求时都保存加密/解密。

然后:

static QueryManager()
{

  Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
  ConnectionStringsSection section = config.GetSection("connectionStrings") as 
                                     ConnectionStringsSection;

  if (section.SectionInformation.IsProtected)
  {
    section.SectionInformation.UnprotectSection();
  }            

  myConnectionString = section.ConnectionStrings["DBConnect"].ConnectionString;

  if (unikSignConnectionString == null)
  {
    throw new ConfigurationErrorsException("Database server not configured");
  }
}

这样,连接字符串就会在内存中解密并使用,而不会产生任何问题,并且避免了对 web.config 的许多无用的读写。

The error comes from a design error.

Here is the solution :

  • First, the encryption has to be made externally from the application in order to avoid to save encryption/decryption each time a request to the database is made.

Then :

static QueryManager()
{

  Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
  ConnectionStringsSection section = config.GetSection("connectionStrings") as 
                                     ConnectionStringsSection;

  if (section.SectionInformation.IsProtected)
  {
    section.SectionInformation.UnprotectSection();
  }            

  myConnectionString = section.ConnectionStrings["DBConnect"].ConnectionString;

  if (unikSignConnectionString == null)
  {
    throw new ConfigurationErrorsException("Database server not configured");
  }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文