asp.net GraphServiceClient中的某些方法在Microsoft中不可用,

41zrol4v  于 2022-12-15  发布在  .NET
关注(0)|答案(2)|浏览(158)

我尝试简单地从AAD安全组检索成员,但似乎无法使Graph代码正常工作,而且奇怪的是,某些方法(如.Request)在GraphServiceClient中不可用。下面的代码在.GetAsync方法中崩溃。我不知道问题可能是什么。

using Microsoft.Graph;
using Azure.Identity;

            string tenantId = appSettings.GetValue<string>("TenantId");
            string clientId = appSettings.GetValue<string>("ClientId");
            string clientSecret = appSettings.GetValue<string>("ClientSecret");

            // using Azure.Identity;
            var options = new TokenCredentialOptions
            {
                AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
            };

            var scopes = new[] { "https://graph.microsoft.com/.default" };
            var clientSecretCredential = new ClientSecretCredential(
                tenantId, clientId, clientSecret, options);

            var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

            var groupMembers = await graphClient
                .Groups["XXXXXXXX-72fc-456f-aefd-XXXXXXXX"]
                .Members
                .GetAsync();

Package 参考:

<ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.11" NoWarn="NU1605" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="6.0.11" NoWarn="NU1605" />
    <PackageReference Include="Microsoft.Azure.Management.DataFactory" Version="8.0.0" />
    <PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.6.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Graph" Version="5.0.0-preview.14" />
    <PackageReference Include="Microsoft.Identity.Client" Version="4.48.1" />
    <PackageReference Include="Microsoft.Identity.Web" Version="2.0.7-preview" />
    <PackageReference Include="Microsoft.Identity.Web.UI" Version="1.16.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.10" />
    <PackageReference Include="Microsoft.Data.SqlClient" Version="5.0.1" />
  </ItemGroup>
avwztpqn

avwztpqn1#

您正在使用Microsoft.Graph程序包的预览版本“5.0.0-preview.14”,该程序包在调用Graph API时删除了Request()节。

我尝试通过运行相同的代码在我的环境中重现相同的结果,但得到以下结果

我创建了一个控制台应用程序,并安装了相同版本的软件包,如下所示:

当我运行与您相同的代码时,我的代码也在**.GetAsync**方法处崩溃,如下所示:

using Microsoft.Graph;
using Azure.Identity;

            string tenantId = "TenantId";
            string clientId = "ClientId";
            string clientSecret = "ClientSecret";

            // using Azure.Identity;
            var options = new TokenCredentialOptions
            {
                AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
            };

            var scopes = new[] { "https://graph.microsoft.com/.default" };
            var clientSecretCredential = new ClientSecretCredential(
                tenantId, clientId, clientSecret, options);

            var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

            var groupMembers = await graphClient
                .Groups["XXXXXXXXXXXXXXXX"]
                .Members
                .GetAsync();

答复:

要找出问题的确切位置,您可以在代码中添加**try-catch**块,如下所示:

using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models.ODataErrors;

string tenantId = "TenantId";
string clientId = "ClientId";
string clientSecret = "ClientSecret";

// using Azure.Identity;
var options = new TokenCredentialOptions
{
    AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};

var scopes = new[] { "https://graph.microsoft.com/.default" };
var clientSecretCredential = new ClientSecretCredential(
    tenantId, clientId, clientSecret, options);

var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

try
{
    var groupMembers = await graphClient
    .Groups["xxxxx-xxxx-xxxxxx-xxxxx"]
    .Members
    .GetAsync();
}
catch (ODataError odataError)
{
    Console.WriteLine(odataError.Error.Code);
    Console.WriteLine(odataError.Error.Message);
    throw;
}

答复:

在我的案例中,我错过了对应用程序中添加的API权限的同意。
为了解决错误,我同意这些权限,如下所示:



选择后,状态将更改为“已授予”,如下所示:

在您的情况下,包之间存在冲突,因为您同时具有Microsoft.Graph并将服务依赖项连接到Microsoft Identity Platform。由于您已修复问题,请使用Microsoft.Graph的稳定版本,而不是使用***预览***版本,因为仍在进行的更新很少。

**参考:**msgraph-sdk-dotnet/升级到v5 - GitHub

bxjv4tth

bxjv4tth2#

我发现了这个问题。我安装了Microsoft.Graph包,并且还连接了Microsoft身份平台的服务依赖项。我卸载了Microsoft.Graph包,Microsoft.Graph命名空间突然有了我所期望的方法和属性。也许有人可以向我解释这个奇怪的地方。

相关问题