如何使用代码(.Net Core)更新Azure App Configuration设置

huangapple go评论108阅读模式
英文:

How to update Azure App Configuration Settings using Code (.Net Core)

问题

我在Azure门户上注册了一个应用程序,该应用程序是使用.NET Core 2.0创建的。该应用程序从门户中的**“应用程序设置”**部分读取一些配置值,如下图所示。

现在在某个阶段,我想从代码中更新这些配置值。我已经搜索了很多文章,但没有找到如何使用.NET Core 2.0的C#来更新Azure**“应用程序设置”的方法。有没有人有想法或建议,我该如何更新Azure“应用程序设置”**?

英文:

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.
如何使用代码(.Net Core)更新Azure App Configuration设置

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

答案1

得分: 3

这可以通过使用 Azure.ApplicationModel.Configuration NuGet 包来实现。
特别是,SetConfigurationSetting 方法似乎可以满足您的需求。

string connectionString = "<connection_string>";
var client = new ConfigurationClient(connectionString);
ConfigurationSetting setting = client.SetConfigurationSetting("some_key", "new_value");

注意:此包处于预览阶段。

英文:

This can be accomplished using Azure.ApplicationModel.Configuration nuget package.
In particular, the SetConfigurationSetting method seems to do what you are after.

string connectionString = &quot;&lt;connection_string&gt;&quot;;
var client = new ConfigurationClient(connectionString);
ConfigurationSetting setting = client.SetConfigurationSetting(&quot;some_key&quot;,&quot;new_value&quot;);

Note: This package is in preview

答案2

得分: 1

你可以使用C#手动调用REST API Web Apps - Update Application Settings

使用以下请求:PUT https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{name}/config/appsettings?api-version=2019-08-01

关于如何在C#中调用Azure REST API的更多详细信息,请参考此链接

英文:

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.

答案3

得分: 0

如果您想使用C#来完成这个任务,您可以尝试使用Microsoft.Azure.Management.Fluent包,以下是示例代码,您可以试一试。

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/***"; // 从 WebApp 获取 -&gt; 属性 -&gt; 资源 ID

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

webapp.Update()
    .WithAppSetting("test", "testvalue")
    .Apply();
英文:

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 = &quot;*******&quot;;
            string clientSecret = &quot;********&quot;;
            string clientId = &quot;********&quot;;
            string subscriptionId = &quot;*******&quot;;
            
            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 = &quot;/subscriptions/**********/resourcegroups/*******/providers/Microsoft.Web/sites/***&quot;; //Get From WebApp -&gt; Properties -&gt; Resource ID

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

            webapp.Update()
                .WithAppSetting(&quot;test&quot;, &quot;testvalue&quot;)
                .Apply();

答案4

得分: 0

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

英文:

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

答案5

得分: 0

这是对George Chen的回答的补充。

为了避免在调用_azure.WebApps.GetById(appResourceId)后出现“操作返回了无效的状态代码'Forbidden'”异常,您需要确保与Azure凭据关联的服务主体具有对Web应用所在的订阅的贡献者访问权限。有关更多详细信息,请参阅https://learn.microsoft.com/en-us/azure/active-directory/develop/howto-create-service-principal-portal。

英文:

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.

huangapple
  • 本文由 发表于 2020年1月6日 18:07:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610116.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定