1
  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 Answers1

1

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();
Steve Michelotti
  • 5,223
  • 1
  • 25
  • 30
  • Hey Thanks for the answer, that works perfectly with latest version of the package, I am using 1.16.1 version of the package, [link](https://www.nuget.org/packages/Microsoft.Azure.Management.ResourceManager.Fluent/1.16.1) When i tred your above code with this version it says that is is not able to find Azure.Configure() line #3 in your code. I wanted to confirm two things, 1. Does 1.16.1 version supports listing resources from Azure Government Cloud 2. How do we create a instance of ResourceManagementClient class for Azure Government Cloud with this version? – Rishikesh Jadhav Oct 25 '19 at 07:21
  • Yes, you can use the latest version of the SDK with my code - which is what I'd recommend. Regarding the ResourceManagementClient, yes you can use that too - I added that alternative implementation to my edited answer. You should be able to easily verify both versions run as stated. Then you can mark answer complete. – Steve Michelotti Oct 26 '19 at 11:49
  • Hi Steve, I have a production app which is using 1.16.1 version of the DLL and both of the implementation that you have provided are not supported with this version of the DLL. As my app is in production, I just wanted to check if it is possible to read Azure Government resources using ResourceManagementClient from version 1.16.1 of the DLL. PS: It works perfectly with the latest version of the package. – Rishikesh Jadhav Oct 27 '19 at 16:16
  • There will be no issue. The syntax might look slightly different that what I have above since it's an old version, but all concepts the same. – Steve Michelotti Oct 30 '19 at 12:37