簡體   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