How can I change a setting in appsettings.json after auto-deploy?
Asked Answered
L

4

13

I have an ASP.NET Core application going on an have setup Github auto-deploy on it. But since it's an open repo I obviously don't want to upload my correct configuration file.

What I'd like to do is to replace some strings in the appsettings.json after a github auto deploy.

"AppSettings": {
    "Token": "my super duper secret token"
}

How can I change my super duper secret token to my real token after a github deploy on Azure?

Lead answered 23/10, 2016 at 19:9 Comment(0)
B
26

As I know we can config token in App Settings on the Azure port. I do a test on this, it works successfully, the following is my detail steps.

  1. Create an Asp.net core Application.
  2. Add [AppSettings] section in the appsetting.json file (Token vaule: mysecretkey). enter image description here
  3. Add a public class AppSettings.cs under the created project. enter image description here
  4. Add the code services.Configure<AppSettings>(Configuration.GetSection("AppSettings")) in the function ConfigureService function in the Startup.cs file (For .net Core 1.0).

Note:The syntax for model binding has changed from RC1 to RC2. Using services.Configure<AppSettings>(Configuration.GetSection("AppSettings")), is no longer availableIn order to bind a settings class to your configuration you need to configure this in the ConfigureServices method of Startup.cs: services.Configure<AppSettings>(options => Configuration.GetSection("AppSettings").Bind(options));

enter image description here 5. Add code to the HomeController.cs file. enter image description here

  1. Publish the WebApp to the Azure Portal.
  2. Add [AppSettings: Token] in the Azure Portal. enter image description here
  3. Browse the WebApp and select the about tab to see the token value is that the value set in the portal. enter image description here
Basophil answered 24/10, 2016 at 8:48 Comment(5)
Nice post, and using the token works fine for me but its part 7 that is the issue here. Before I set it to AppSettings.Token and changed it to use : instead. But when I FTP into my application I can clearly see that the value hasn't been updated. Any ideas?Lead
@staticelf Yes, AppSettings:Token should be used. It also can be found in the screenshot in the part 7. After add the appsetting value in the Azure portal, it doesn't overwrite the original value in the appsetting.json file.The data set in the appsetting in the Azure portal will be retrieved at runtime by code running inside of a website. More detail please refer to document .Basophil
I was able to manage the settings directly within the "Publish" settings in VS 2017, there is a link "Edit App Service Settings" that brings up a window to enter AppSettings:Token directly, without going to the Azure portal in the browser.Barna
I am getting an error saying App setting names can only contain letters, numbers (0-9), periods ("."), and underscores ("_") How you all managed? I am using asp.net core 3.1Colatitude
6 years on and this is still helpful :) @Uwe, that's also really useful, thanks. In VS2022 it's called "Manage Azure AppService settings" and you can get to it using the ... in the Hosting section of the Publish settings.Imperception
C
4

Assuming the web site already exists as a resource in Azure, you can simply set the App Settings/Connection strings in the portal. These will override the ones in the appsettings.json file at runtime. Ie. your app will first look at the azure app settings/connection strings before looking for them in the local file. This is part of asp.net core's "cloud first" approach to configuration management. These settings wont get overwritten when you deploy code to the app/slot.

Found a blog post here which describes it in a bit more detail, using the .AddEnvironmentVariables() call to add azure slot settings to the configuration.

Chloroprene answered 24/10, 2016 at 5:35 Comment(0)
T
1

There is a code editing functionality in developer tools settings (Settings -> Development Tools -> App Service Editor (Preview)). You can go there and change any file you like in there. But you probably will need to restart the web application (by editing web.config or some other way).. You can also use Kudu (Advanced Tools) for that, but it's not as pleasant UI as Visual Studio Code in the first option.

Though the more advanced and correct way of dealing with application secrets is the special secret manager. You can read more about it on asp.net documentation here.

Generally it's a way to load the secrets from a protected data storage and override them with environmental variables in production (can be set in azure web app).

Teddy answered 23/10, 2016 at 19:26 Comment(0)
U
1

If you are using Azure DevOps Release to deploy, you can easily specify properties for each environment/stage. You can use the task File Transform and indicate the path to appsettings.json: Azure DevOps Release - Task File transform

Or if you are deploying directly to Azure:

Azure DevOps Release - Task File Azure App Service Deploy

So you just need to create the variables to override the data in the settings:

Azure DevOps Release Variables

Unyielding answered 28/11, 2019 at 18:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.