blazormicrosoft-graph-api.net-8.0microsoft-graph-sdks

Blazor app hangs after request MS Graph Me.GetAsync()


I'm doing a simple call to the Me endpoint. If I step through to await graphClient.Me.GetAsync() that line, it jumps out to the calling method and the browser is just spinning. It seems to me that Graph is throwing an exception but it must be swallowing it because nothing is caught.

Any suggestions why this is happening

Here's the method:

public async Task<User> GetGraphMeAsync(GraphServiceClient graphClient)
{
    try
    {
        var graphUser = await graphClient.Me.GetAsync().ConfigureAwait(false);
        if(graphUser != null)  
            return graphUser;

        else { return new User(); }
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message); 
    }
}

I'm calling it from OnInitialeAsync() of the same component.

        protected override async Task OnInitializedAsync()
        {
            var graphClient = GetGraphClient();
            var meData = await GetGraphMeAsync(graphClient).ConfigureAwait(false);
        }

My next test is to switch from the SDK to the API.

UPDATE

I made the changes @tiny-wang outlined. I'm having an issue with the packages. The AddAuthentication() chain throws an error at .addMicrosoftGraph(). It is: Reference to type 'IAuthenticationProvider' claims it is defined in 'Microsoft.Graph.Core', but it could not be found

enter image description here Everything I can find about this error is 1-1/2 years old and points to an issue upgrading Microsoft.Graph from 4.xx to 5.0. We're currently on 5.58 so using 4.xx doesn't seem to make sense. Anyone know any recent issues?

UPDATE

Based on this discussion (How to utilize GraphServiceClient for dependency injection in application based permissions) where someone else running .Net 8 experienced a similar error, I removed the Microsoft.Graph and Microsoft.Graph.Core packages but the error persists.

UPDATE I tried a new project template and it works fine, which is what I expected. I removed all references to any "Idenity" or "Graph" packages from the project file. I added them back in, one by one, and now the error is gone. ! I've got a couple more things to look at but I'm closing this. Thanks for the help!


Solution

  • I had a test in my side which worked well. Firstly, I followed this section to integrate Azure AD into my blazor web app server side rendered application. Because the Graph SDK you used is graphClient.Me.GetAsync(), it requires the app to integrate MSAL library so that users can sign into the application and your code could know who is Me. Then according to your code snippet, we could see that you are using V5 Graph SDK, then please adding nuget packages below for integrating Graph SDK into the application.

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

    If we only need to integrate Azure AD authentication, we only need to have builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme).AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd")) , but if we also want to generate access token, we need to have

    builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
        .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"))
        .EnableTokenAcquisitionToCallDownstreamApi() 
        .AddInMemoryTokenCaches();
    

    If we also want to integrate Graph SDK, then we should have

    builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
        .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"))
        .EnableTokenAcquisitionToCallDownstreamApi()
        .AddMicrosoftGraph()
        .AddInMemoryTokenCaches();
    

    Then let's inject GraphServiceClient into the component.

    @page "/counter"
    @using Microsoft.Identity.Web
    @rendermode InteractiveServer
    @inject ITokenAcquisition TokenAcquisitionService
    @using Microsoft.Graph
    
    <PageTitle>Counter</PageTitle>
    <h1>Counter</h1>
    
    @code {
        [Inject]
        GraphServiceClient GraphClient { get; set; }
    
        protected override async Task OnInitializedAsync()
        {
            var me = await GraphClient.Me.GetAsync();
            var token = await TokenAcquisitionService.GetAccessTokenForUserAsync(new string[] { "User.Read" });
        }
    }
    

    Here's my test result.

    enter image description here