如何使用 Azure .NET Fluent SDK 获取 Azure App Service TLS 配置信息?

How to get Azure App Service TLS configuration information using Azure .NET Fluent SDK?

我正在尝试使用 C# 中的 Azure Fluent .NET SDK 在应用服务 (WebApp) 上配置 TLS 版本。

我使用以下代码使用 LINQPad 获取 WebApp 信息:

    // Nuget : Install-Package Microsoft.Azure.Management.AppService.Fluent -Version 1.24.0
    // using Microsoft.Azure.Management.AppService.Fluent;
    // using Microsoft.Azure.Management.AppService.Fluent.Models;
    // using Microsoft.Azure.Management.ResourceManager.Fluent;

    // Using a Service Principal created on my Azure Subscription
    var clientId = "<service_principal_clientid_with_readaccess>";
    var tenantId = "<my_azuread_tenantid>";

    var credentials = SdkContext
        .AzureCredentialsFactory
        .FromServicePrincipal(clientId, 
            Util.GetPassword("azure-cli-secret"), // LINQPad helper, replace if needed 
            tenantId, 
            AzureEnvironment.AzureGlobalCloud);

    // Get one web app with TLS 1.2 configured
    var webApp = AppServiceManager
        .Authenticate(credentials, "<azure_subscription_id>")
        .WebApps
        .GetById("<webapp_resource_id>");

    // Dump object returned, LINQPad helper method
    webApp.Dump();

当转储对象模型返回时,我在任何地方都找不到返回的 TLS 信息。应该可用的东西,因为它是由 REST API 返回的。

分析

您可以使用 https://resources.azure.com 或直接在此端点上看到信息可用:

https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Web/sites/{appServiceName}/config?api-version=2018-02-01

这将为您提供以下输出(此处缩短了属性列表):

{
  "value": [
    {
      "id": "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Web/sites/{appServiceName}/config/web",
      "name": "{appServiceName}",
      "type": "Microsoft.Web/sites/config",
      "location": "West Europe",
      "properties": {
        "http20Enabled": false,
        "minTlsVersion": "1.2",
        "ftpsState": "AllAllowed",
        "reservedInstanceCount": 0,
        "preWarmedInstanceCount": null,
        "healthCheckPath": null
      }
    }
  ],
  "nextLink": null,
  "id": "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Web/sites/{appServiceName}/config"
}

然后我查看了此处可用的 SDK 代码源:https://github.com/Azure/azure-libraries-for-net. I found that the TLS version should be available in the SiteConfig 对象。此 SiteConfig 似乎位于 Inner 属性 内部。

查看我们使用 LINQPad 获得的对象转储时,我们可以看到未填充 SiteConfig 对象:

调试此代码时,使用断点,我们可以看到信息在内部似乎可用,但未映射到 SiteConfig 属性 可用 public内属性.

我们看到非 public 成员 _siteConfig 填充的调试会话屏幕截图:

这是一个错误还是我遗漏了什么?
有什么想法吗?

是的。如果您使用这种方式,siteConfig 将始终 return null。这是设计使然。如果您想获取相关信息,可以使用以下方法:

            AzureCredentials credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(
            clientId,
            clientSecret,
            tenantId,
            AzureEnvironment.AzureGlobalCloud).WithDefaultSubscription(subscriptionId);

        RestClient restClient = RestClient
            .Configure()
            .WithEnvironment(AzureEnvironment.AzureGlobalCloud)
            .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
            .WithCredentials(credentials)
            .Build();


        ResourceManagementClient resourceManagementClient = new ResourceManagementClient(restClient);
        WebSiteManagementClient webSiteManagementClient = new WebSiteManagementClient(restClient);
        webSiteManagementClient.SubscriptionId = subscriptionId;
        var configs = webSiteManagementClient.WebApps.GetConfigurationAsync("<rg name>", "<app name>").Result;
        var minTls = configs.MinTlsVersion;
        Console.WriteLine(minTls);

合并此 PR 后,属性 MinTlsVersion 应该可以直接在 webApp 上使用。 https://github.com/Azure/azure-libraries-for-net/pull/1023