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>
2
Answers
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.
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:
When I ran the same code as you, my code also crashed at
.GetAsync
method as below:Response:
To find out where exactly the issue lies, you can add
try-catch
block in your code like below:Response:
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:
After selecting Yes, Status will be changed to Granted like below:
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 ofMicrosoft.Graph
instead of using preview version as there are few updates still going on.Reference: msgraph-sdk-dotnet/upgrade-to-v5 – GitHub