Asp.net mvc 如何将web.config中的加密连接字符串添加到我的DBContext mvc4?

Asp.net mvc 如何将web.config中的加密连接字符串添加到我的DBContext mvc4?,asp.net-mvc,entity-framework,asp.net-mvc-4,connection-string,Asp.net Mvc,Entity Framework,Asp.net Mvc 4,Connection String,我在MVC4项目中使用UsersContext作为dbcontext。我在Web.config中加密了连接字符串,因为站点位于共享服务器中。我的背景是: Public Class UsersContext Inherits DbContext Public Sub New() MyBase.New("DefaultConnection") End Sub Public Property UserProfiles As DbSet(Of U

我在MVC4项目中使用UsersContext作为dbcontext。我在Web.config中加密了连接字符串,因为站点位于共享服务器中。我的背景是:

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

    Public Property UserProfiles As DbSet(Of UserProfile)
End Class
“DefaultConnection”在web.config中加密。
如何使用解密函数和初始化连接检测

   'I want to do Something like this : 

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

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

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

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

正确的方法是使用Aspnet_regiis.exe工具对节进行加密。不需要提供您自己的加密或解密逻辑。见官员

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


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