从代码更改Azure的网站应用程序设置从代码更改Azure的网站应用程序设置(Change Azur

2019-05-12 08:14发布

是否有可能改变应用程序设置为从应用程序本身就是一个网站?

这并不意味着是一种日常的操作,但自我服务重新配置选项。 非开发人员可以更改特定的设置,这应该引起重新开始,就像我可以手动做网站的配置页面(应用程序设置部分)

Answer 1:

这并不是说难,一旦我找到了正确的lib做到这一点, 微软Azure网站管理库 。

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


Answer 2:

您还可以使用Azure的流畅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();
}


Answer 3:

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.



Answer 4:

除了迭戈的回答,使用Web应用程序中的Azure管理Librairies(网站和/或WebJobs),你需要配置SSL这是有点棘手:

从Azure的网上工作使用Azure管理库



文章来源: Change Azure website app settings from code