Home > OS >  Some methods in GraphServiceClient not available Microsoft.Graph
Some methods in GraphServiceClient not available Microsoft.Graph

Time:12-14

I'm attempting to simply retrieve members from an AAD security group and can't seem to get my Graph code to function and, strangely, some methods such as .Request are not available in GraphServiceClient. The code below crashes in the .GetAsync method. I'm lost as to what the issue might be.

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 references:

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

CodePudding user response:

I figured out the issue. I had Microsoft.Graph package installed and also a connected service dependency to Microsoft identity platform. I uninstalled the Microsoft.Graph package and suddenly the Microsoft.Graph namespace had the methods and properties I expected. Maybe someone can explain to me this oddity.

CodePudding user response:

You are using preview version '5.0.0-preview.14' of Microsoft.Graph package that removed Request() section from it while calling Graph API.

I tried to reproduce the same in my environment by running same code and got below results

I created one Console application and installed packages of same version as you like below:

enter image description here

When I ran the same code as you, my code also crashed at .GetAsync method as below:

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();

Response:

enter image description here

To find out where exactly the issue lies, you can add try-catch block in your code like below:

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

Response:

enter image description here

In my case, I missed granting consent to the added API permissions in the application.

To resolve the error, I granted consent to those permissions as below:

enter image description here

After selecting Yes, Status will be changed to Granted like below:

enter image description here

In your case, there is a conflict between packages as you have both Microsoft.Graph and connected service dependency to Microsoft identity platform. As you already fixed your issue, use stable versions of Microsoft.Graph instead of using preview version as there are few updates still going on.

Reference: msgraph-sdk-dotnet/upgrade-to-v5 - GitHub

  • Related