2

I am trying to update only one application setting using below request. My setting is getting updated properly, but my all other application settings are vanished. I see only one settings there with the correct updated value which I tried to update. I do not want to loose or change all other application settings.

What am I missing here or what is wrong I am doing?

I am following the below given article:

https://learn.microsoft.com/en-us/rest/api/appservice/webapps/updateapplicationsettings

PUT https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{name}/config/appsettings

I am using their online tool to send the request: https://learn.microsoft.com/en-us/rest/api/appservice/webapps/updateapplicationsettings

Since I am using the online tool it is generating the authorization token. But I want to do programmatically. It would be great if I can get the sample code to generate the token and to update application settings.

Authorization: Bearer
eyJ0eXAiOixxxxxxxeyE_rd3Cw
Content-type: application/json

Joey Cai
  • 18,968
  • 1
  • 20
  • 30
  • What the language you want? – Joy Wang Mar 19 '19 at 01:30
  • Thanks for replying back and I am looking for C# code. Also I want to mention one more thing which I should have included as part of my original question. The reason I am trying to update the application setting is that I want to disable my azure function programatically in case network is down or some other exception occurs. I wonder if there is some other simpler/direct way to disable the function programatically(C#). – Pradeep Chaudhary Mar 19 '19 at 13:51
  • I have a similar issue trying to apply application settings with an ARM template. It seems that the API to manage appconfig is not idempotent. https://stackoverflow.com/questions/59383303/arm-templates-for-azure-functions-with-many-appsettings-for-different-environmen I raised a github issue against azure cli https://github.com/Azure/azure-cli/issues/11718 – Anthony Klotz Jan 06 '20 at 21:45

1 Answers1

0

I reproduce your problem and if you want to update application setting, you need to write down all the application settings, otherwise it will be overridden by the one application setting.

Preparation:

1.Register an App registration in Azure Active Directory and get appid and appsecret. Please refer to this article.

2.Add the registered app into Role assignments under Access control.

enter image description here

Here is C# code sample you could refer to.

var appId = "xxxxxxxxxxxxxxxxxxxx";
var secretKey = "xxxxxxxxxxxxxxxxxxxx";
var tenantId = "xxxxxxxxxxxxxxxxxxx";
var context = new AuthenticationContext("https://login.windows.net/" + tenantId);
ClientCredential clientCredential = new ClientCredential(appId, secretKey);
var tokenResponse = context.AcquireTokenAsync("https://management.azure.com/", clientCredential).Result;
var accessToken = tokenResponse.AccessToken;
using (var client = new HttpClient())
{
    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
    var baseUrl = new Uri($"https://management.azure.com/");
    var requestURl = baseUrl +
                    @"subscriptions/xxxxxxxxxxxxxxxxxxx/resourceGroups/xxxxxx/providers/Microsoft.Web/sites/xxxxxx/config/appsettings?api-version=2016-08-01";
    string body = "{\"kind\": \"webapp\",\"properties\": {\"WEBSITE_NODE_DEFAULT_VERSION\": \"6.9.1\",\"aaa\": \"bbb\"}}";
    var stringContent = new StringContent(body, Encoding.UTF8, "application/json");
    var response = client.PutAsync(requestURl, stringContent).Result;
}

The result is as below:

enter image description here

Joey Cai
  • 18,968
  • 1
  • 20
  • 30
  • The reason I am trying to update the application setting is that I want to disable my azure function programatically in case network is down or some other exception occurs. I wonder if there is some other simpler/direct way to disable the function programatically(C#). – – Pradeep Chaudhary Mar 19 '19 at 13:57
  • Does this [article](https://learn.microsoft.com/en-us/azure/azure-functions/disable-function) that you want? And you could also set it in [host.json](https://github.com/Azure/azure-functions-host/wiki/host.json) to set you want to enable function like `"functions": [ "QueueProcessor", "GitHubWebHook" ]` – Joey Cai Mar 20 '19 at 02:05
  • Environment variable does not change by changing the host.json. It just shows that function is disable since in 2.x version, host.json is only for design purpose. It probably works with 1.x version. I am able to change the host.json but nothing really gets changed except design/cosmetic change. Also the article you mentioned talks about how to change variable value from azure portal not from the C# program. – Pradeep Chaudhary Mar 23 '19 at 23:40