加密ASP.NET连接字符串的正确方法是什么?

我一直在查看ASP.NET MVC应用程序(.NET 4.0)中加密connectionStrings(Web.config)的几个示例,似乎有两种通用方法可以实现它( 示例1和相关示例2 ):

使用aspnet_regiis工具。

使用aspnet_regiis的主要问题是我可以在本地(在我的开发机器上)运行该工具,但该网站实际上托管在Arvixe上,就像任何其他Web主机一样:无法在服务器上运行命令。 据我所知,如果我在我的机器上加密Web.config connectionStrings并发布Web.config,那么它们就无法在服务器上解密(如果这是错误的,请纠正我)。

注意:我只使用了RSAProtectedConfigurationProvider ,但我认为DataProtectionConfigurationProvider会有相同的问题,因为它是用户/机器特定的。

以编程方式加密连接字符串。

以编程方式加密connectionStrings也有一个缺点:每次我发布我的网站时都会更新Web.config(使用未加密的connectionStrings),这意味着在一段时间内Web.config将不会被加密。 即使我确保只在发生更改时发布Web.config,问题可能会最小化但不会减轻。

我认为使用静态类可以进一步帮助减少connectionStrings未加密的时间。 不幸的是,加密connectionStrings需要应用程序路径,似乎获取应用程序路径的唯一方法是来自请求( Request.ApplicationPath ),而在静态类中,(显然)没有请求。

 private void ProtectSection(string sectionName, string provider) { Configuration config = WebConfigurationManager. OpenWebConfiguration(Request.ApplicationPath); ConfigurationSection section = config.GetSection(sectionName); if (section != null && !section.SectionInformation.IsProtected) { section.SectionInformation.ProtectSection(provider); config.Save(); } } private void UnProtectSection(string sectionName) { Configuration config = WebConfigurationManager. OpenWebConfiguration(Request.ApplicationPath); ConfigurationSection section = config.GetSection(sectionName); if (section != null && section.SectionInformation.IsProtected) { section.SectionInformation.UnprotectSection(); config.Save(); } } UnProtectSection("appSettings"); ProtectSection("appSettings", "DataProtectionConfigurationProvider"); 

加密connectionString并消除/减少Web.config部分未加密的时间的正确方法是什么? 你是否围绕OpenWebConfiguration方法编写了一些类型的包装器,它检查该部分是否已加密并仅使用该包装器? 用户可以访问您网站上的任何页面,那么如何在数据库中请求某些数据之前延迟加密,或者您是否在第一时间检查它是否已加密?

使用共享主机的最简单方法可能是编写自己的加密/解密实用程序,并将连接字符串存储在应用程序设置,自定义XML文件或文本文件中,以便您可以完全控制加密/解密过程…

根据您的更新,您可以通过以下方式获取当前请求:

 new HttpRequestWrapper(System.Web.HttpContext.Current.Request); 

HTH。

我想出了一个自己的解决方案,但我希望有更好的方法来做到这一点:

 internal static class SecurityExtension { public static string GetConnetionString(this Configuration config, string databaseName, string provider = "RSAProtectedConfigurationProvider") { string sectionName = "connectionStrings"; ConfigurationSection section = config.GetSection(sectionName); if (section != null && !section.SectionInformation.IsProtected) { section.SectionInformation.ProtectSection(provider); config.Save(); } return WebConfigurationManager.ConnectionStrings[databaseName].ConnectionString; } } 

现在我只需要在每次想要获取配置字符串时使用扩展:

 Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); string connectionString = config.GetConnetionString("MyDatabaseName"); 

更新:
感谢Brian,我们可以静态获取连接字符串:

 private static HttpRequestWrapper request = new HttpRequestWrapper(System.Web.HttpContext.Current.Request); private static Configuration config = WebConfigurationManager.OpenWebConfiguration(request.ApplicationPath); private static string connectionString = config.GetConnetionString("MyDatabaseName")); 

当然,您不希望存储类似的连接字符串,而只是使用它来初始化DataContext或将其用于您需要的任何其他内容。

Web.config文件获取ConnectionString

在开始如何从web.config文件中读取或获取connectionstring之前,您应该知道如何获取连接字符串以从此处连接数据库,这将显示我在连接字符串中未使用用户ID和密码的区别。 知道这一点后,您可以在web.config中调整连接字符串,如下所示。

    

要从asp.net中的web.config文件中读取或获取connectionstring,我们需要添加System.Configuration命名空间,以帮助我们读取连接字符串。

注意:如果您已在appSettings部分下定义了连接字符串,则可以通过这种方式从appSettings部分获取您的连接字符串。 在C#中读取Connectionstring

 protected void Page_Load(object sender, EventArgs e) { string con = System.Configuration.ConfigurationManager.ConnectionStrings["myDbConnection"].ConnectionString; } 

在Vb.net中读取Connectionstring

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim con As String = System.Configuration.ConfigurationManager.ConnectionStrings("myDbConnection").ConnectionString End Sub 

示例:Asp.net中的简单插入,更新和删除您可能还喜欢在asp.net gridview上插入,更新和删除。 就是这样,现在你可以从web.config文件中获取连接字符串,并且可以在任何你想要的地方使用。

您可以通过http://www.aspneto.com/category/aspnet/在asp.net上阅读更多内容。