您需要确保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();