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