Search code examples
c#azureconsole-applicationazure-sdk-.netazure-resource-graph

Operation returned an invalid status code 'BadRequest' - Microsoft.Azure.Management.ResourceGraph


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}");
    }
}

Solution

  • 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