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

旧时模样 提交于 2019-12-23 01:54:36

问题


I'm getting an Error 500 due to missing AppId and AppSecret on my Azure websites deployment.

How do I set this up on the server? Copying the project.json file seems to be insufficient.

On the development machine, the AppId and AppSecret values were added to configuration through SecretManager.

UPDATE: I have now hardcoded the AppId and AppSecret values in code as the way its done in previous versions and of course this still works. Eventually, I'd like to still be able to use SecretManager (or something similar) for setting the config values on the server, for obvious security reasons.


回答1:


Secrets in Azure Web Apps (Websites) are stored as app settings (and connection strings). In your code you should use System.Configuration.ConfigurationManager.AppSettings to access them.

You need to specify a local/debug setting in web.config file.

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

And the actual secrets are configured in the Azure management portal as APP SETTINGS.




回答2:


This was troubling me also. The documentation is unclear. However, I discovered the solution when trying to figure out the same issue with the connection string:

https://stackoverflow.com/a/31341718/567524

(You can use the code there to display your config settings from Azure website and debug them, which is how I figured this out.)

Basically, MVC 6 no longer uses web.config so Azure does not work in the same way. Instead, Azure App Settings are available through the Environment Variables:

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

Now environment variables from Azure are mapped to specific keys. For example, an Azure connection string setting becomes:

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

This is great because the default MVC 6 template uses the same pattern Data:NAME:ConnectionString.

Also, for our app settings, if we use a ":" delimiter the environment variable gets mapped to the expected place.

The Azure app setting called "Authentication:Facebook:AppId" will overwrite the config.json value:

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

The key point is that all of this is passed from Azure to MVC 6 through the environment variables. (Which is why the AddEnvironmentVariables() is the last call on the configuration to ensure that it has priority over other values.)



来源:https://stackoverflow.com/questions/30438382/setting-asp-net-5-authentication-appid-and-appsecret-configuration-in-azure-webs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!