3

I'm upgrading an application from .NET Core 2.2 to .NET 5.0. In the process I needed to update both Microsoft.Graph and Microsoft.Identity.Web. I updated to use the new configuration however when I try to get user details Graph throws System.NullReferenceException.

This is my service configuration

services.AddMicrosoftIdentityWebApiAuthentication(configuration)
    .EnableTokenAcquisitionToCallDownstreamApi()
    .AddMicrosoftGraph(configuration.GetSection("MicrosoftGraph"))
    .AddInMemoryTokenCaches();

My appsettings.json does have the needed values

"AzureAd": {
    "ClientId": "xxx.xxx......",
    "Domain": "domain.com",
    "Instance": "https://login.microsoftonline.com/",
    "TenantId": "xxxxxx......",
    "ClientSecret": "xxxxxxxxx...."
},
"MicrosoftGraph": {
    "BaseUrl": "https://graph.microsoft.com/v1.0",
    "Scopes": "user.read user.read.all"
}

And this is the code that calls the graph API

var user = await _graphServiceClient.Users["username"].Request().GetAsync();

The code works on the current application where I configure Graph using the old interface and getting a silent token using an old version of Microsoft.Identity.Web. Not sure why with the new version it seems to be unable to obtain the Graph token. Authentication in general for the application works and I do have an authenticated user, it only fails when calling MS Graph.

Here is a log entry for reference of the error

Method: Handle. Line: 73. Code: generalException
Message: An error occurred sending the request.

Status Code: 0
Microsoft.Graph.ServiceException: Code: generalException
Message: An error occurred sending the request.

---> 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.GetAuthenticationResultForUserAsync(IEnumerable1 scopes, String authenticationScheme, String tenantId, String userFlow, ClaimsPrincipal user, TokenAcquisitionOptions tokenAcquisitionOptions) at Microsoft.Identity.Web.TokenAcquisitionAuthenticationProvider.AuthenticateRequestAsync(HttpRequestMessage request) at Microsoft.Graph.AuthenticationHandler.SendAsync(HttpRequestMessage httpRequestMessage, CancellationToken cancellationToken) at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
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.UserRequest.GetAsync(CancellationToken cancellationToken)

Thank you

Fernando
  • 614
  • 1
  • 9
  • 20
  • 2
    `I'm upgrading an application from .NET Core 2.2 to 3.1` why? .NET 5 is out and .NET 6 will be available by December. – Ian Kemp Sep 17 '21 at 09:09
  • No LTS yet, mistake we made with 2.2 and don't want to repeat. – Fernando Sep 17 '21 at 13:07
  • .NET 6 is going to be LTS. The change from 5 to 6 is going to be far smaller than going from 3.1 to 6. Therefore, it makes the most sense to migrate to 5 now, then do a smaller migrate after 6 is released. And anyway, LTS means nothing anymore - except that if your software is older than LTS you might as well declare it legacy. – Ian Kemp Sep 17 '21 at 14:23
  • Upgraded to .NET 5 and still same issue. – Fernando Sep 17 '21 at 20:53
  • Were you able to solve same? – Anuj Nautiyal Feb 02 '22 at 12:13
  • No, still experiencing the same issue even after updating web identity to 1.23 and graph 4.11 – Fernando Feb 17 '22 at 12:17

0 Answers0