1
  1. 我正在尝试使用 Microsoft.Azure.Management.ResourceManager.Fluent 命名空间下 .NET SDK 类 ResourceManagementClient 的以下代码片段来发现 Azure 政府资源

    new ResourceManagementClient(creds).Resources.ListAsync()

  2. 发现我收到错误,因为找不到“订阅”。

  3. 当我们尝试使用相同的代码在 Azure 公共环境中发现资源时,这非常有效

Azure Government 的 .NET SDK 有什么问题吗?还是因为 Azure 政府服务不提供 Azure Resource Graph 服务?

4

1 回答 1

1

您需要确保AzureEnvironment.AzureUSGovernment为您的AzureCredentials(下面的第 2 行)指定如下:

var servicePrincipal = new ServicePrincipalLoginInformation { ClientId = "<your-client-id>", ClientSecret = "<your-client-secret" };
var creds = new AzureCredentials(servicePrincipal, tenantId: "<your-tenant-id>", AzureEnvironment.AzureUSGovernment);
var azure = Azure.Configure().Authenticate(creds).WithDefaultSubscription();
var rgs = await azure.ResourceGroups.ListAsync();

或者,您可以使用ResourceManagementClient上面的代码执行相同的操作,尽管ResourceManagementClient代码更冗长,所以我的建议是在大多数情况下您想要使用上面的代码,但这里是替代方案:

var servicePrincipal = new ServicePrincipalLoginInformation { ClientId = "<your-client-id>", ClientSecret = "<your-client-secret" };
var creds = new AzureCredentials(servicePrincipal, tenantId: "<your-tenant-id>", AzureEnvironment.AzureUSGovernment);
var restClient = RestClient
    .Configure()
    .WithEnvironment(AzureEnvironment.AzureUSGovernment)
    .WithCredentials(creds)
    .Build();
var resourceManagementClient = new ResourceManagementClient(restClient);
resourceManagementClient.SubscriptionId = "<your-subscription-id>";
var rgs = await resourceManagementClient.ResourceGroups.ListAsync();
于 2019-10-17T18:20:40.590 回答