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

时间:2011-02-22 02:34:07

标签: c# asp.net encryption connection-string

我一直在查看ASP.NET MVC应用程序(.NET 4.0)中加密connectionStrings(Web.config)的几个示例,似乎有两种通用的方法来实现它(example 1和相关的example 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方法周围编写了某种类型的包装器,以检查该部分是否已加密且仅使用该包装器?用户可以访问您网站上的任何页面,那么如何在数据库中请求某些数据之前延迟加密,或者您是否在第一时间检查它是否已加密?

3 个答案:

答案 0 :(得分:3)

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

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

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

HTH。

答案 1 :(得分:0)

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

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或将其用于您需要的任何其他内容。

答案 2 :(得分:-1)

Web.config文件

获取ConnectionString

在开始如何从web.config文件中读取或获取connectionstring之前,你应该知道如何从这里获取连接数据库的连接字符串,这也将告诉你为什么我没有在我的用户ID和密码中使用连接字符串。知道这一点后,您可以在web.config中调整连接字符串,如下所示。

<connectionStrings>
   <add name="myDbConnection" providerName="System.Data.SqlClient"
   connectionString="Data Source=myServer;Integrated Security=true;Initial Catalog=myDatabase"/>
</connectionStrings>

要从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上阅读更多内容
相关问题