9

Is it possible to change the app settings for a website from the app itself?

This is not meant to be an everyday operation, but a self-service reconfiguration option. A non-developer can change a specific setting, which should cause a restart, just like I can do manually on the website configuration page (app setting section)

Thomas
  • 24,234
  • 6
  • 81
  • 125
Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154

4 Answers4

12

You can also use the Azure Fluent Api.

using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Authentication;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;

...

public void UpdateSetting(string key, string value)
{
    string tenantId = "a5fd91ad-....-....-....-............";
    string clientSecret = "8a9mSPas....................................=";
    string clientId = "3030efa6-....-....-....-............";
    string subscriptionId = "a4a5aff6-....-....-....-............";

    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/xxx/resourceGroups/xxx/providers/Microsoft.Web/sites/xxx"; //Get From WebApp -> Properties -> Resource ID

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

    //Set App Setting Key and Value
    webapp.Update()
        .WithAppSetting(key, value)
        .Apply();
}
Niall
  • 121
  • 1
  • 4
  • I got compile time error on line `var _azure = Azure.Configure()...` `Azure` not found, Do I need to install any other nuget packages to resolve it? – prog1011 Jan 06 '20 at 12:04
  • You shouldn't need anything other than the references I typed above. – Niall Mar 19 '20 at 16:16
  • Might need to fully qualify the name Microsoft.Azure.Management.Fluent.Azure – rjacobsen0 Apr 12 '22 at 00:12
  • Plus I needed to install nuGet packages Microsoft.Azure.Management.Fluent and Microsoft.Azure.Management.AppService.Fluent (for app services) – rjacobsen0 Apr 12 '22 at 01:20
9

It wasn't that hard once I found the right lib to do it, Microsoft Azure Web Sites Management Library.

var credentials = GetCredentials(/*using certificate*/);
using (var client = new WebSiteManagementClient(credentials))
{
    var currentConfig = await client.WebSites.GetConfigurationAsync(webSpaceName,
                                                                    webSiteName);
    var newConfig = new WebSiteUpdateConfigurationParameters
                    {
                        ConnectionStrings = null,
                        DefaultDocuments = null,
                        HandlerMappings = null,
                        Metadata = null,
                        AppSettings = currentConfig.AppSettings
    };
    newConfig.AppSettings[mySetting] = newValue;
    await client.WebSites.UpdateConfigurationAsync(webSpaceName, webSiteName,
                                                   newConfig);
}
Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
  • Can you please guide me from where I can get `webSpaceName` and `webSiteName` from the azure portal? – prog1011 Jan 06 '20 at 12:51
  • @Diego Mijelshon, do you know if we would be able to update the "General Settings" to Java. – Darey Oct 18 '20 at 16:30
1

Have you read into the Service Management REST API? The documentation mentions that it allows you to perform most the actions that are available via the Management Portal programmatically.

Jamie Edge
  • 121
  • 1
  • 6
  • That, however, helped me get to the management libraries, which I'm now checking out – Diego Mijelshon May 26 '14 at 16:55
  • It looks like this is now supported on the REST API - https://learn.microsoft.com/en-us/rest/api/appservice/webapps/updateapplicationsettings – abrown Nov 21 '18 at 10:02
0

In addition to Diego answer, to use the Azure Management Librairies within a WebApp (WebSites and/or WebJobs), you need to configure SSL which is a little bit tricky:

Using Azure Management Libraries from Azure Web Jobs

Thomas
  • 24,234
  • 6
  • 81
  • 125