1
votes
  1. I am trying to discover Azure Government resources using below code snippet from .NET SDK class ResourceManagementClient under Microsoft.Azure.Management.ResourceManager.Fluent namespace

    new ResourceManagementClient(creds).Resources.ListAsync()

  2. While discovering i am getting error as ‘The subscription ‘' could not be found.’

  3. This works perfectly fine when we try to discover resource on Azure Public environment using same code

Is there any issue with the .NET SDK with Azure Government? or is it because the Azure Resource Graph service is not available with Azure Government Services?

1

1 Answers

1
votes

You need to make sure you specify the AzureEnvironment.AzureUSGovernment for your AzureCredentials (line #2 below) like this:

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

Alternatively, you can use ResourceManagementClient to do the same as the code above, though the ResourceManagementClient code is more verbose so my recommendation is in most cases you want to use the code above, but here is the alternative:

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