Setting ASP.Net 5 Azure网站中的认证AppId和AppSecret配置

Setting ASP.Net 5 Authentication AppId and AppSecret Configuration in Azure Websites

由于我的 Azure 网站部署缺少 AppId 和 AppSecret,我收到错误 500。

如何在服务器上进行设置?复制 project.json 文件似乎不够。

在开发机器上,AppId 和 AppSecret 值是 added to configuration through SecretManager

更新:我现在已经在代码中硬编码了 AppId 和 AppSecret 值,就像它在以前版本中所做的那样,当然这仍然有效。最终,出于明显的安全原因,我仍然希望能够使用 SecretManager(或类似的东西)在服务器上设置配置值。

Azure Web 应用程序(网站)中的机密存储为应用程序设置(和连接字符串)。 在您的代码中,您应该使用 System.Configuration.ConfigurationManager.AppSettings 来访问它们。

您需要在 web.config 文件中指定 local/debug 设置。

<appSettings>
    <add key="AppId" value="appid" />
</appSetting>

而实际的机密在 Azure 管理门户中配置为 APP SETTINGS

这也让我很困扰。文档不清楚。但是,我在尝试找出与连接字符串相同的问题时发现了解决方案:

(您可以使用那里的代码从 Azure 网站显示您的配置设置并调试它们,这就是我想出的方法。)

基本上,MVC 6 不再使用 web.config,因此 Azure 不会以相同的方式工作。相反,Azure 应用程序设置可通过环境变量获得:

// Get the environment variables (which is how we will access Azure App Settings)
configuration.AddEnvironmentVariables()

现在 Azure 中的环境变量被映射到特定的键。例如,Azure 连接字符串设置变为:

// The Azure Connection string called "NAME" will be accessible here in MVC 6
Data:NAME:ConnectionString

这很棒,因为默认的 MVC 6 模板使用相同的模式 Data:NAME:ConnectionString。

此外,对于我们的应用程序设置,如果我们使用“:”分隔符,环境变量将映射到预期位置。

名为 "Authentication:Facebook:AppId" 的 Azure 应用程序设置将覆盖 config.json 值:

"Authentication": {
    "Facebook": {
        "AppId": "123MyId",...

关键是所有这些都是通过环境变量从 Azure 传递到 MVC 6 的。 (这就是为什么 AddEnvironmentVariables() 是对配置的最后一次调用,以确保它优先于其他值。)