aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

ASP.NET Core Web App (Model-View-Controller) targeting .NET 8 with Windows Identity Platform fails after user login

Open jpremill opened this issue 2 years ago • 4 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Describe the bug

When Creating a New ASP.NET Core Web App (Model-View-Controller) 8 targeting .NET 8 with Windows Identity Platform and checking the allow graph api checkbox, the application fails after the user is selected from the authentication user interface; it throws the following error:

InvalidOperationException: Unable to resolve service for type 'Microsoft.Graph.GraphServiceClient' while attempting to activate '.Controllers.HomeController'.

Expected Behavior

a starter template should build and run as expected; when the user logs in as their user using the authentication interface, it should succeed and the rest of the application should load without error.

Steps To Reproduce

1, Create new ASP.NET Core Web App (Model-View-Controller) project targeting .NET 8 with authentication type set to Windows Identity Platform 2. following configuration prompts for azure integration to an app registration 3. check the enable Graph API permissions box. 4. build and run the application. 5. Use the Microsoft Authentication interface to perform the login action. 6. after the user is selected or the login is processed, the error is thrown.

Exceptions (if any)

InvalidOperationException: Unable to resolve service for type 'Microsoft.Graph.GraphServiceClient' while attempting to activate '.Controllers.HomeController'.

.NET Version

8.0.100-rc.2.23502.2

Anything else?

The authenticaton guidance is extremely confusing. Admittedly, the release of .net 8 isnt until next week, but the existing quickstarts for Microsoft Entra seem extremely outdated and do not seem to be applicable at all. Is there is an updated doc page I am just missing ?

I'd really like to see a template project variation for the react and asp.net that has a project with the server and a project with the react app in typescript that is setup so that the server does microsoft entra authentication for a react SPA. Are there special preview templates somewhere ?

jpremill avatar Nov 07 '23 16:11 jpremill

Thanks for reporting this, @jpremill. @vijaykrishnadongala do you own the WIP integration code that is being generated by VS? I feel like the issue here is that the service registration isn't being generated.

mkArtakMSFT avatar Nov 09 '23 21:11 mkArtakMSFT

I fixed the problem by changing the builder.Services.AddAuthentication in Program.cs. // Add services to the container. builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme) .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd")) .EnableTokenAcquisitionToCallDownstreamApi() .AddMicrosoftGraph(builder.Configuration.GetSection("MicrosoftGraph")) .AddInMemoryTokenCaches();

Brettus-ntt avatar Jan 30 '24 01:01 Brettus-ntt

Thanks @Brettus-ntt. @jpremill can you confirm if @Brettus-ntt 's suggestion works for you?

mkArtakMSFT avatar Mar 13 '24 16:03 mkArtakMSFT

Hi @jpremill. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

See our Issue Management Policies for more information.