【问题标题】:how to add encrypted connection string in web.config to my DBContext mvc4?如何将 web.config 中的加密连接字符串添加到我的 DBContext mvc4?
【发布时间】:2014-06-04 09:23:13
【问题描述】:

我在 mvc 4 项目中将 UsersContext 作为 dbcontext。我有Web.config 中的加密连接字符串,因为站点位于共享服务器。我的 DbContext 是:

Public Class UsersContext
    Inherits DbContext
      Public Sub New()
        MyBase.New("DefaultConnection")
      End Sub

    Public Property UserProfiles As DbSet(Of UserProfile)
End Class

“默认连接”在 web.config 中已加密。
我如何使用我的解密函数并初始化连接。

   'I want to do Something like this : 

  Public Sub New()
    MyBase.New(MyStringDecrpytor(ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString))
  End Sub

其中 MyStringDecrpytor 是将连接字符串解密为原始文本的函数。

【问题讨论】:

  • 你的密码在哪里?
  • 它的公共共享函数和解密连接字符串。

标签: asp.net-mvc entity-framework asp.net-mvc-4 connection-string


【解决方案1】:

您可以在派生的构造函数中以编程方式设置连接字符串,而不是依赖基础 DbContext。只需将DbContext.Database.Connection.ConnectionString 设置为您想要的任何内容。在 VB.NET 中,我相信它会是:

Public Sub New()
  MyBase.New()
  My.Database.Connection.ConnectingString = DecryptFunction("encryptedstringhere")
End Sub

【讨论】:

    【解决方案2】:

    执行此操作的正确方法是使用 Aspnet_regiis.exe 工具对部分进行加密。无需提供您自己的加密或解密逻辑。见官方Microsoft Documentation

    您可以使用 ASP.NET IIS 注册工具 (Aspnet_regiis.exe) 来加密或解密 Web 配置文件的各个部分。处理 Web.config 文件时,ASP.NET 将自动解密加密的配置元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-31
      • 2012-06-18
      • 2011-08-19
      • 1970-01-01
      • 2010-12-14
      • 1970-01-01
      • 2018-09-23
      • 2011-06-21
      相关资源
      最近更新 更多