2

I have an Azure app registered on Azure portal which is created in .NET Core 2.0 This app reads some config value from the Application settings section from the portal as shown in below image. enter image description here

Now at some stage I want to update those config value from code. I have searched for many article but not found how to update Azure Application settings from code. Can any one have an idea or suggestion that How can I update Azure Application settings using .NET Core 2.0 C#?

1
  • Could you solve it with provided solution? Commented Jan 16, 2020 at 6:40

5 Answers 5

3

This can be accomplished using Azure.ApplicationModel.Configuration nuget package. In particular, the SetConfigurationSetting method seems to do what you are after.

string connectionString = "<connection_string>";
var client = new ConfigurationClient(connectionString);
ConfigurationSetting setting = client.SetConfigurationSetting("some_key","new_value");

Note: This package is in preview

Sign up to request clarification or add additional context in comments.

2 Comments

This works only for Configuration Store I think. Or there is a way to use it with existing web app Application settings? What is connection_string for it then?
@arteny For the connection string, inject IConfiguration, then, assuming you've followed the instructions here, you can use configuration.GetConnectionString("AppConfig").
1

You could use c# to call the REST API Web Apps - Update Application Settings manually.

PUT https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{name}/config/appsettings?api-version=2019-08-01

For more details about how to call Azure REST API in c#, you could refer to this link.

Comments

0

If you want to use c# to do it, you could try with Microsoft.Azure.Management.Fluent package, the below is the sample code, you could have a try.

            string tenantId = "*******";
            string clientSecret = "********";
            string clientId = "********";
            string subscriptionId = "*******";

            var azureCredentials = new AzureCredentials(new
              ServicePrincipalLoginInformation
            {
                       ClientId = clientId,
                       ClientSecret=clientSecret
            }, tenantId, AzureEnvironment.AzureGlobalCloud) ;
            var _azure = Azure
           .Configure()
           .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
           .Authenticate(azureCredentials)
           .WithSubscription(subscriptionId);

            var appResourceId = "/subscriptions/**********/resourcegroups/*******/providers/Microsoft.Web/sites/***"; //Get From WebApp -> Properties -> Resource ID

            var webapp = _azure.WebApps.GetById(appResourceId);

            webapp.Update()
                .WithAppSetting("test", "testvalue")
                .Apply();

3 Comments

Hi George, I have tried your code, it gave me an exception (Operation returned an invalid status code 'Forbidden') , what should I do to solve it
Did you ever solve you 'Operation returned an invalid status code 'Forbidden' problem? I've run into the same problem
Unfortunately this does not work, and as the other two posters have suggested that this produces a 403 forbidden response.
0

The library has been changed to Azure.Data.AppConfiguration. Azure.ApplicationModel.Configuration is depracated

Comments

0

This is an addendum to George Chen's answer.

To avoid the "Operation returned an invalid status code 'Forbidden' exception after calling _azure.WebApps.GetById(appResourceId), you need to ensure the service principal associated with the Azure Credential has contributor access to the subscription the web app is in. For more details refer to https://learn.microsoft.com/en-us/azure/active-directory/develop/howto-create-service-principal-portal.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.