繁体   English   中英

我应该在哪里存储我的 ASP.NET Core 应用程序生产环境的连接字符串?

[英]Where should I store the connection string for the production environment of my ASP.NET Core app?

部署到 IIS 7(不是 Azure)时,生产和暂存连接字符串应该存储在 ASP.NET Core 应用程序中的什么位置?

我正在寻找推荐的做法/最佳实践,尤其是安全方面。

在 ASP.NET 5 中,可以指定多个配置源。 由于对先前模型的这一受欢迎的更改,您可以将开发连接字符串存储在简单的 json 文件中,并将暂存和生产连接字符串直接存储在相应服务器上的环境变量中。

如果您像这样配置您的应用程序:

var config = new Configuration()
.AddJsonFile("config.json")
.AddEnvironmentVariables();

并且在 config.json 和环境变量中都有连接字符串,那么环境源将获胜。

因此,将您的开发连接字符串存储在 config.json(并在源代码管理中自由检入)并将生产连接字符串存储在环境变量中。 更多信息在这里这里

您想在开发中使用用户机密 API。 请参阅下面的示例(基于 ASP.NET 5 Beta 5):

ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(
    applicationEnvironment.ApplicationBasePath);

// This reads the configuration keys from the secret store. This allows you to store 
// connection strings and other sensitive settings on your development environment, so you 
// don't have to check them into your source control provider.
if (hostingEnvironment.IsDevelopment())
{
    configurationBuilder.AddUserSecrets();
}

IConfiguration configuration = configurationBuilder.Build();

您还必须在 project.json 中设置 userSecretsId(请参阅下面的链接)。 这是您的项目机密的唯一 ID,在创建新机密时将在下面用到:

{
  "webroot": "wwwroot",
  "userSecretsId": "[Your User Secrets ID]",
  "version": "1.0.0-*"
  ...
}

要添加/删除/更新您的用户机密,您需要通过运行以下命令安装机密管理器:

dnu commands install SecretManager

然后使用秘密管理器实际添加/删除/更新您的设置:

Usage: user-secret [options] [command]
Options:
  -?|-h|--help  Show help information
  -v|--verbose  Verbose output
Commands:
  set     Sets the user secret to the specified value
  help    Show help information
  remove  Removes the specified user secret
  list    Lists all the application secrets
  clear   Deletes all the application secrets
Use "user-secret help [command]" for more information about a command.

有关更多信息,请参阅文档和文档。

我还应该注意,在撰写本文时(ASP.NET 5 Beta 5),用户机密并未加密! 这将在以后改变。

暂无
暂无

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

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