I have to use the Microsoft Graph SDK in a Blazor web app on .NET 8, but it doesn’t seem implemented yet, is there a working example?
I’m trying on Visual Studio 2022 with packages Microsoft.Graph 5.58.0 and Microsoft.Graph.Core 3.1.21
MC94 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
For blazor web app interactive server render mode application, we can use codes below to integrate Azure AD and Graph SDK,then we can inject GraphServiceClient
into the component like @using Microsoft.Graph @inject GraphServiceClient GraphClient
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"))
.EnableTokenAcquisitionToCallDownstreamApi()
.AddMicrosoftGraph()
.AddInMemoryTokenCaches();
My test sample has nuget packages below.
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.0" NoWarn="NU1605" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="8.0.0" NoWarn="NU1605" />
<PackageReference Include="Microsoft.Graph" Version="5.56.0" />
<PackageReference Include="Microsoft.Graph.Core" Version="3.1.17" />
<PackageReference Include="Microsoft.Identity.Web" Version="3.1.0" />
<PackageReference Include="Microsoft.Identity.Web.GraphServiceClient" Version="3.1.0" />
<PackageReference Include="Microsoft.Identity.Web.UI" Version="3.1.0" />
</ItemGroup>
The sample above requires users to sign in first so that the Graph Client can get authorized and call Graph API. If you don’t want a sign-in step, then you could learn about Azure Ad client credential flow. And this requires us to consent Application type permission. And you can use codes below in your component.
using Microsoft.Graph;
using Azure.Identity;
var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "tenantId";
var clientId = "clientId";
var clientSecret = "clientSecret";
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var res = await graphClient.Users.GetAsync();