更新应用程序设置:Azure功能

时间:2019-03-18 13:44:36

标签: azure azure-web-sites

我尝试使用以下请求仅更新一个应用程序设置。我的设置正在正确更新,但是所有其他应用程序设置都消失了。我在那里仅看到一个设置,该设置具有我尝试更新的正确更新值。我不想丢失或更改所有其他应用程序设置。

我在这里想念的是什么?我在做什么错了?

我正在关注以下给出的文章:

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

MongoError: not authorized on <DB> to execute command

我正在使用他们的在线工具发送请求: https://docs.microsoft.com/en-us/rest/api/appservice/webapps/updateapplicationsettings

由于我使用的是在线工具,因此它会生成授权令牌。但我想以编程方式进行。如果我能获得示例代码来生成令牌并更新应用程序设置,那就太好了。

  

授权:不记名
  eyJ0eXAiOixxxxxxxeyE_rd3Cw
  内容类型:application / json

1 个答案:

答案 0 :(得分:0)

我重现了您的问题,如果您想更新应用程序设置,则需要写下所有应用程序设置,否则它将被一个应用程序设置覆盖。

准备工作

在Azure Active Directory中

1。Register an App registration并获取appid和appsecret。请参阅此article

2。将注册的应用添加到Role assignments下的Access control中。

enter image description here

这是您可以参考的C#代码示例。

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;
}

结果如下:

enter image description here