简体   繁体   English

在环境变量 ASP.Net MVC 应用程序中正确存储数据库连接字符串

[英]Correctly storing db connection strings in environment variables ASP.Net MVC Apps

I am trying to move my 2 database connection strings to environment variables for security reasons.出于安全原因,我正在尝试将我的 2 个数据库连接字符串移动到环境变量。 Everything works fine when I include the 2 connection strings on web.config like so:当我在 web.config 中包含 2 个连接字符串时,一切正常,如下所示:

<connectionStrings>
    <clear />
    <add name="DefaultConnection" connectionString="Data Source=xxxxxx" providerName="System.Data.SqlClient" />
    <add name="RDSContext" connectionString="Data Source=xxxxxx" providerName="System.Data.SqlClient" />
 </connectionStrings>

I then removed the 2 connection strings from web.config and created 2 environment variables as follows:然后,我从 web.config 中删除了 2 个连接字符串,并创建了 2 个环境变量,如下所示:

setx CUSTOMCONNSTR_DefaultConnection "Data Source=xxxxx" setx CUSTOMCONNSTR_RDSContext "Data Source=xxxxx" setx CUSTOMCONNSTR_DefaultConnection "Data Source=xxxxx" setx CUSTOMCONNSTR_RDSContext "Data Source=xxxxx"

Although I now get the following error when I startup IIS and visit the web app虽然我现在在启动 IIS 并访问 web 应用程序时出现以下错误

Server Error in '/' Application. Cannot attach the file 'C:\Users\xxx\xxx\App_Data\BookingSystem.Models.RDSContext.mdf' as database 'BookingSystem.Models.RDSContext'.

Can anyone tell me what I am doing incorrect?谁能告诉我我做错了什么?

Simply naming your environment variable in a certain way doesn't mean that MVC will pick them up.简单地以某种方式命名您的环境变量并不意味着 MVC 会选择它们。 You may be assuming a little more magic is happening than there actually is.您可能会假设发生的魔法比实际发生的要多一些。

When you do ConfigurationManager.ConnectionStrings["MyConnectionStringName"].ConnectionString you're only retrieving the connection string from your web.config file, or machine.config if you happened to have defined it there.当您执行ConfigurationManager.ConnectionStrings["MyConnectionStringName"].ConnectionString时,您只是从 web.config 文件或 machine.config 中检索连接字符串(如果您碰巧在此处定义了它)。 There's nothing there to tell your app to grab it from anywhere else.没有什么可以告诉您的应用程序从其他任何地方获取它。

Instead, you might consider creating a class to represent your configuration:相反,您可以考虑创建一个 class 来表示您的配置:

public class MyApplicationDatabaseConfiguration
{
    public string ConnectionString { get; set; }
}

Any class that needs to obtain this connection string can do so by depending on an instance of that MyApplicationDatabaseConfiguration.任何需要获取此连接字符串的 class 都可以通过依赖于该 MyApplicationDatabaseConfiguration 的实例来实现。

public class MyDatabaseRepository
{
    readonly MyApplicationDatabaseConfiguration _dbConfig;

    public MyDatabaseRepository(MyApplicationDatabaseConfiguration dbConfig)
    {
        _dbConfig = dbConfig;
    }

    public void DoSomethingWithTheDatabase()
    {
         using(var connection = new SqlConnection(_dbConfig.Connectionstring))
         {
             //now you can use the connection
         }
    }
}

Then you can load your config however you like.然后你可以加载你喜欢的配置。 Ex:前任:

MyApplicationDatabaseConfiguration dbConfig = new MyApplicationDatabaseConfiguration();
dbConfig.ConnectionString = Environment.GetEnvironmentVariable("MyApplication_MyConnectionString");

Now the MyDatabaseRepository doesn't care how you load the database config, it simply says "I need the database config".现在 MyDatabaseRepository 不关心你如何加载数据库配置,它只是说“我需要数据库配置”。 This is extremely powerful, it allows you to change out the configuration by simply changing a line of code.这非常强大,它允许您通过简单地更改一行代码来更改配置。 Need to change back to using web.config?需要改回使用 web.config 吗? It's as simple as dbConfig.ConnectionString = ConfigurationManager.ConnectionStrings["MyConnectionStringName"].ConnectionString;它就像dbConfig.ConnectionString = ConfigurationManager.ConnectionStrings["MyConnectionStringName"].ConnectionString;

Microsoft has taken this one step further in Microsoft.Extensions.Configuration library, which is meant for .NET Core but targeted to .NET Standard, so you can use it in your .NET Framework library if you'd like . Microsoft 在 Microsoft.Extensions.Configuration 库中更进一步,它适用于 .NET Core 但针对 .NET Standard,因此如果您愿意,可以在 .NET Framework 库中使用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM