0

每当var response行执行时,它都会抛出一个异常,指出

操作返回了无效的状态代码“BadRequest”

我在 Azure Resource Graph Explorer 中执行的相同请求按预期工作。但是出于某种奇怪的原因,当我在运行 .net 6 框架的 .net 控制台应用程序中执行此操作时,它告诉我这是一个错误的请求,而实际上它是正确的请求。但是,在显示该错误后,它说Principal used: IsAuthenticated:True Type:User TenantId: xxxxx UserPrincipalName: xxxx

安装的软件包:

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

程序.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}");
    }
}
4

1 回答 1

1

我已尝试在我的环境中重新创建上述问题,并且遇到与您相同的问题。

使用 Console 应用程序创建了一个新项目,.net6并将您的代码复制到我的本地 .

已安装nuget packages:-

Microsoft.Azure.Management.ResourceGraph" 版本="2.1.0" Microsoft.Azure.Services.AppAuthentication" 版本="1.6.2"

这是我的.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>

当我尝试使用您的代码时,我得到Operation returned an invalid status code 'BadRequest'在此处输入图像描述

将查询参数类型query: "resources | project name, ['type'] | limit 5" 更改为此 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}");
    }
}

它在我的最后工作正常: 在此处输入图像描述

如果您遇到同样的问题,请参考此Microsoft 文档Run your first Resource Graph query using .NET Core

有关更多信息,请参阅此MICROSOFT PLAYGROUND通过资源图 SDK 连接到资源图客户端

于 2022-01-19T10:26:07.083 回答