0

Whenever var response line executes it will throw an exception that states

Operation returned an invalid status code 'BadRequest'

The same request that I did in Azure Resource Graph Explorer worked as expected. But for some odd reason, when I do it in a .net console application running a .net 6 framework it tells me its a bad request when in fact it is the correct request. However, after displaying that error it says Principal used: IsAuthenticated:True Type:User TenantId: xxxxx UserPrincipalName: xxxx

Packages installed:

<PackageReference Include="Microsoft.Azure.Management.ResourceGraph" Version="2.1.0" />
    <PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.6.2" />

Program.cs

using Microsoft.Azure.Management.ResourceGraph;
using Microsoft.Azure.Management.ResourceGraph.Models;
using Microsoft.Azure.Services.AppAuthentication;
using Microsoft.Rest;

AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();
RunResourceGraphQuery(azureServiceTokenProvider).Wait();
if (azureServiceTokenProvider.PrincipalUsed != null)
{
    Console.WriteLine($"{Environment.NewLine}Principal used: {azureServiceTokenProvider.PrincipalUsed}");
}

Console.ReadLine();

static async Task RunResourceGraphQuery(AzureServiceTokenProvider azureServiceTokenProvider)
{
    Console.WriteLine($"{Environment.NewLine}{Environment.NewLine}Please enter the subscription Id");
    var subscriptionId = Console.ReadLine();
    List<string> subscriptions = new();
    subscriptions.Add(subscriptionId);

    try
    {
        var tokenCredentials = new TokenCredentials(await azureServiceTokenProvider.GetAccessTokenAsync("https://management.azure.com/").ConfigureAwait(false));
        var resourceGraphClient = new ResourceGraphClient(tokenCredentials);
        var userQueryRequest = new QueryRequest(subscriptions: subscriptions, query: "resources | project name, ['type'] | limit 5", null, null);

        var response = resourceGraphClient.Resources(userQueryRequest);
        Console.WriteLine(response);

    }
    catch (Exception exp)
    {
        Console.WriteLine($"Error message: {exp.Message}");
    }
}
MarkCo
  • 810
  • 2
  • 12
  • 29

1 Answers1

1

I have tried in my environment to recreate the above issue ,and i get the same issue as you are getting.

Created a new project using Console app with .net6 and copied your code to my local .

Installed nuget packages :-

Microsoft.Azure.Management.ResourceGraph" Version="2.1.0" Microsoft.Azure.Services.AppAuthentication" Version="1.6.2"

This is my .csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Management.ResourceGraph" Version="2.1.0" />
    <PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.6.2" />
  </ItemGroup>

</Project>

When i tried with your code i get Operation returned an invalid status code 'BadRequest': enter image description here

After changing the query parameter type query: "resources | project name, ['type'] | limit 5" to this query: "Resources | project name, type | limit 5"

Program.cs:-

using Microsoft.Azure.Management.ResourceGraph;
using Microsoft.Azure.Management.ResourceGraph.Models;
using Microsoft.Azure.Services.AppAuthentication;
using Microsoft.Rest;

AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();
RunResourceGraphQuery(azureServiceTokenProvider).Wait();
if (azureServiceTokenProvider.PrincipalUsed != null)
{
    Console.WriteLine($"{Environment.NewLine}Principal used: {azureServiceTokenProvider.PrincipalUsed}");
}

Console.ReadLine();

static async Task RunResourceGraphQuery(AzureServiceTokenProvider azureServiceTokenProvider)
{
    Console.WriteLine($"{Environment.NewLine}{Environment.NewLine}Please enter the subscription Id");
    var subscriptionId = Console.ReadLine();
    List<string> subscriptions = new();
    subscriptions.Add(subscriptionId);

    try
    {
        var tokenCredentials = new TokenCredentials(await azureServiceTokenProvider.GetAccessTokenAsync("https://management.azure.com/").ConfigureAwait(false));
        var resourceGraphClient = new ResourceGraphClient(tokenCredentials);
        var userQueryRequest = new QueryRequest(subscriptions: subscriptions, query: "Resources | project name, type | limit 5", null, null);

        var response = resourceGraphClient.Resources(userQueryRequest);
        Console.WriteLine(response);

    }
    catch (Exception exp)
    {
        Console.WriteLine($"Error message: {exp.Message}");
    }
}

Its working fine at my end: enter image description here

Still if you are getting the same issue please refer this Microsoft Documentation:Run your first Resource Graph query using .NET Core

For more information refer this MICROSOFT PLAYGROUND : Connecting to the Resource Graph Client via the Resource Graph SDK

AjayKumarGhose
  • 4,257
  • 2
  • 4
  • 15
  • Hello @MarkCo, Could you please try to create a new `console application` . I used VS 2022. and follow the steps – AjayKumarGhose Jan 19 '22 at 14:35
  • Hi, I did, I was able to get it to work! Just used an incorrect subscription id. Thank you! Question in the first article you sent "Run your first Resource Graph" (https://learn.microsoft.com/en-us/azure/governance/resource-graph/first-query-dotnet#run-your-first-resource-graph-query), the clientId and tenantId is from an app registration correct? If so, does a key vault need to exist to allow policies to that app registration so when I perform the query search it will allow it and not return bad request? – MarkCo Jan 19 '22 at 14:41
  • 1
    yes we have to use our Azure app registration credential . – AjayKumarGhose Jan 19 '22 at 14:50