【问题标题】:Staging environment does not take the correct connection string Asp.Net Core暂存环境不采用正确的连接字符串 Asp.Net Core
【发布时间】:2016-03-16 09:34:46
【问题描述】:

暂存环境未采用正确的连接字符串。

我有三个环境。开发、分期和发布。 我的应用设置如下所示:

{
 "Data": {
  "DefaultConnection": {
  "ConnectionString": "My connection string"
}
},
 "Logging": {
  "IncludeScopes": false,
  "LogLevel": {
  "Default": "Verbose",
  "System": "Information",
  "Microsoft": "Information"
  }
 }
}

我还有两个名为 appsettings.Staging.json 和 appsettings.Release.json 的文件。 在项目属性下,我制作了两个配置文件:Staging 和 Release,它们都有一个 Hosting:环境,其 Value Staging in staging 和 Release in Release。它在 VS 中运行良好,但是当我将它上传到 Azure 时,它​​总是使用我的 localhost 连接字符串而不是我的在线数据库的默认 appsettings。 我究竟做错了什么?

【问题讨论】:

    标签: environment-variables asp.net-core-mvc appsettings


    【解决方案1】:

    如果连接字符串在文本文件中包含敏感数据,则不应存储连接字符串,而应直接在 Azure 中的 AppSettings 中设置值以用于生产(或查看类似 Key Vault 的内容)来设置值。我建议阅读https://docs.asp.net/en/latest/security/app-secrets.htmlhttps://docs.asp.net/en/latest/fundamentals/configuration.html

    要解决从正确设置文件中读取值的问题,请参见下文。

    您需要在 Azure 中设置环境值。在 Azure 门户中,在您的 WebApp 上转到 All Settings->Application Settings->AppSettings 并添加设置 ASPNET_ENVHosting:Environment,其值为 Staging(或您感兴趣的任何其他环境。

    要读取appsettings.Staging.json 文件,您可以执行以下操作:

    【讨论】:

      猜你喜欢
      • 2021-07-15
      • 2020-08-08
      • 2017-12-09
      • 2019-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-18
      • 1970-01-01
      相关资源
      最近更新 更多