Repository navigation
Description
Which version of Microsoft Identity Web are you using?
1.15.2
Where is the issue?
- Web app
- Sign-in users
- Sign-in users and call web APIs
- Web API
- Protected web APIs (validating tokens)
- Protected web APIs (validating scopes)
- Protected web APIs call downstream web APIs
- Token cache serialization
- In-memory caches
- Session caches
- Distributed caches
- Other (please describe)
Is this a new or an existing app?
The app is in production (using 1.10.0) and I have upgraded to a new version of Microsoft Identity Web.
Repro
Debug Launch is set to Project.
Launch Browser is unticked.
The below code works fine with 1.10.0.
Anything above 1.10.0 (1.11-1.15.2) throws a NullReferenceException.
Startup.cs
services.AddMicrosoftIdentityWebApiAuthentication(Configuration) .EnableTokenAcquisitionToCallDownstreamApi() .AddMicrosoftGraphAppOnly(authProvider => new GraphServiceClient(authProvider)) .AddInMemoryTokenCaches();
Shortened code from IHostedService:
private async void DoWork(object state) { using (var scope = Services.CreateScope()) { var graphClient = scope.ServiceProvider .GetRequiredService<GraphServiceClient>(); var users = await graphClient.Users.Request().GetAsync(); } }
Expected behavior
Return the list of users within Azure AD using Graph App Only
Actual behavior
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Identity.Web.MergedOptions.PrepareAuthorityInstanceForMsal()
at Microsoft.Identity.Web.TokenAcquisition.BuildConfidentialClientApplication(MergedOptions mergedOptions)
at Microsoft.Identity.Web.TokenAcquisition.GetOrBuildConfidentialClientApplication(MergedOptions mergedOptions)
at Microsoft.Identity.Web.TokenAcquisition.GetAuthenticationResultForAppAsync(String scope, String authenticationScheme, String tenant, TokenAcquisitionOptions tokenAcquisitionOptions)
at Microsoft.Identity.Web.TokenAcquisitionAuthenticationProvider.AuthenticateRequestAsync(HttpRequestMessage request) at Microsoft.Graph.AuthenticationHandler.SendAsync(HttpRequestMessage httpRequestMessage, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.SendAsyncCore(HttpRequestMessage request, HttpCompletionOption completionOption, Boolean async, Boolean emitTelemetryStartStop, CancellationToken cancellationToken)
at Microsoft.Graph.HttpProvider.SendRequestAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at Microsoft.Graph.HttpProvider.SendRequestAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
at Microsoft.Graph.HttpProvider.SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
at Microsoft.Graph.BaseRequest.SendRequestAsync(Object serializableObject, CancellationToken cancellationToken, HttpCompletionOption completionOption)
at Microsoft.Graph.BaseRequest.SendAsync[T](Object serializableObject, CancellationToken cancellationToken, HttpCompletionOption completionOption)
at Microsoft.Graph.GraphServiceUsersCollectionRequest.GetAsync(CancellationToken cancellationToken)
at TestProject.BackgroundServices.TimedDirectorySynchroniserService.DoWork(Object state) in C:\Users\User\source\repos\TestProject\TestProject\BackgroundServices\TimedDirectorySynchroniserService.cs:line 48
Possible solution
Additional context / logs / screenshots
Activity
paolovalladolid commentedon Aug 6, 2021
I am getting the same exception and stack trace. In my case, this is happening in a console app.
Excerpt from Main method of the console app:
Method that calls Azure using GraphServiceClient
jennyf19 commentedon Aug 11, 2021
paolovalladolid do you have a working repo or link to code that i can download and run locally?
paolovalladolid commentedon Aug 12, 2021
jennyf19 I have created a repo for a simple console app that demonstrates the issue with using GraphServiceClient with the IoC framework. Plug in your Azure settings into the appsettings.json of course.
https://github.com/paolovalladolid/PVSimpleMSGraphConsole
jennyf19 commentedon Aug 17, 2021
paolovalladolid
How do you add the authorization and authentication middleware? (
app.UseAuthentication();andapp.UseAuthorization();) ?microsoft-identity-web/tests/WebAppCallsWebApiCallsGraph/gRPC/Startup.cs
Lines 44 to 45 in 1f46bd9
If you request an app only scope, you want to apply
.WithAppOnly()to the request:paolovalladolid commentedon Aug 17, 2021
jennyf19 I tried to make the suggested code changes but am having a hard time pushing my changes to GitHub because I can't get my new token to work with GitHub. I modified the classes as follows. I still get the same stack trace:
jmprieur commentedon Aug 20, 2021
Thanks @mrfrankbell
I see, so it's not really a protected web API, paolovalladolid ?
12 remaining items
jennyf19 commentedon Aug 27, 2021
I tried to repro this with an anonymous controller and a GRPC service, but could not get the same error. MaxxDelusional could you provide a repro?
MaxxDelusional commentedon Aug 27, 2021
I suspect that I am probably doing something wrong.
In any case, here is a simplified repo. You'll need to modify
appsettings.json, and specify a userId inTestController.https://github.com/MaxxDelusional/IdentityWebTest
jennyf19 commentedon Aug 27, 2021
Thanks for the repro MaxxDelusional, was very helpful. We have a bug when using an anonymous controller, but as a work around, you can specify the auth scheme in the TestController and things should work:
also, in Startup.cs, you need to include:
to set up all the middleware correctly.
Jean-Marc Prieur (@jmprieur) we need to do something better here for when we don't have the token but it is a web API.
mrichards3 commentedon Aug 27, 2021
Thanks jennyf19. We were having this same issue using
IDownstreamWebApi.CallWebApiForAppAsyncfrom an anonymous controller since version 1.10.Adding
app.UseAuthentication(), and then also explicitly passing the authentication scheme like this avoids the NullReferenceException:jmprieur commentedon Aug 27, 2021
Thanks for the investigation and the heads-up jennyf19
Maybe we should leverage IAuthenticationSchemeProvider.GetDefaultAuthenticateSchemeAsync in ITokenAcquisition?
jennyf19 commentedon Aug 29, 2021
MaxxDelusional paolovalladolid Trancesphere the fix is in master if you want to try it out. will be in next release, possibly next week or week after (1st/2nd week of sept).
jennyf19 commentedon Sep 6, 2021
Included in 1.16.1 release