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#?

Brian Hauger
  • 490
  • 1
  • 4
  • 22
prog1011
  • 3,425
  • 3
  • 30
  • 57

5 Answers5

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

Ian Bennett
  • 360
  • 3
  • 10
  • 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 Apr 10 '21 at 06:43
  • @arteny For the connection string, inject `IConfiguration`, then, assuming you've followed the instructions [here](https://learn.microsoft.com/en-us/azure/azure-app-configuration/quickstart-aspnet-core-app?tabs=core5x#connect-to-the-app-configuration-store), you can use `configuration.GetConnectionString("AppConfig")`. – Neo Apr 27 '21 at 19:10
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.

Joy Wang
  • 39,905
  • 3
  • 30
  • 54
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();
George Chen
  • 13,703
  • 2
  • 11
  • 26
  • 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 – Walid Feb 13 '20 at 16:31
  • Did you ever solve you 'Operation returned an invalid status code 'Forbidden' problem? I've run into the same problem – sjokkogutten Aug 28 '20 at 09:28
  • Unfortunately this does not work, and as the other two posters have suggested that this produces a 403 forbidden response. – Simon Price Mar 10 '22 at 11:54
0

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

teeboy
  • 408
  • 3
  • 13
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.