Repository navigation
Description
Which version of Microsoft Identity Web are you using?
Microsoft Identity Web 1.16.0 and Microsoft Identity Web 1.16.1
Where is the issue?
When calling await _graphServiceClient.Users["username"].Request().GetAsync();
With 1.16.0 Get Null Reference Exception
With 1.16.1 Get MsalUiRequiredException
- Web API
- Protected web APIs (validating tokens)
- Protected web APIs (validating scopes)
- [ X] Protected web APIs call downstream web APIs
- Token cache serialization
- [X ] In-memory caches
- Session caches
- Distributed caches
Is this a new or an existing app?
This app is currently in production using .NET Core 2.2. Working on upgrading to .NET 5.0, updating the libraries and now running into this issue.
Repro
Service configuration:
services.AddMicrosoftIdentityWebApiAuthentication(configuration)
.EnableTokenAcquisitionToCallDownstreamApi()
.AddMicrosoftGraph(configuration.GetSection("MicrosoftGraph"))
.AddInMemoryTokenCaches();appsettings.json:
"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"
},Usage of MS Graph
var user = await _graphServiceClient.Users["username"].Request().GetAsync();Expected behavior
Get the requested user details
Actual behavior
The exceptions mentioned above are thrown instead of getting the user details
Additional context / logs / screenshots
1.16.0 - Log Entries
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)
1.16.1 - Log Entries
[INF] Microsoft.Identity.Web.TokenAcquisition: False MSAL 4.36.0.0 MSAL.NetCore .NET Core 3.1.18 Microsoft Windows 10.0.19042 [09-14 15:59:48.88 - 036cf0e8-9260-414d-9fe4-7c74dd014285]
=== Request Data ===
Authority Provided? - True
Scopes - user.read user.read.all
Extra Query Params Keys (space separated) -
ApiId - AcquireTokenSilent
IsConfidentialClient - True
SendX5C - False
LoginHint ? False
IsBrokerConfigured - False
HomeAccountId - False
CorrelationId - 036cf0e8-9260-414d-9fe4-7c74dd014285
[INF] Microsoft.Identity.Web.TokenAcquisition: False MSAL 4.36.0.0 MSAL.NetCore .NET Core 3.1.18 Microsoft Windows 10.0.19042 [09-14 15:59:48.89 - 036cf0e8-9260-414d-9fe4-7c74dd014285] === Token Acquisition (SilentRequest) started:
Scopes: user.read user.read.all
Authority Host: login.microsoftonline.com
[DBG] Microsoft.Identity.Web.TokenAcquisition: False MSAL 4.36.0.0 MSAL.NetCore .NET Core 3.1.18 Microsoft Windows 10.0.19042 [09-14 15:59:48.89 - 036cf0e8-9260-414d-9fe4-7c74dd014285] No account passed to AcquireTokenSilent.
[DBG] Microsoft.Identity.Web.TokenAcquisition: False MSAL 4.36.0.0 MSAL.NetCore .NET Core 3.1.18 Microsoft Windows 10.0.19042 [09-14 15:59:53.52 - 036cf0e8-9260-414d-9fe4-7c74dd014285] Token cache could not satisfy silent request.
[ERR] Microsoft.Identity.Web.TokenAcquisition: False MSAL 4.36.0.0 MSAL.NetCore .NET Core 3.1.18 Microsoft Windows 10.0.19042 [09-14 16:00:10.23 - 036cf0e8-9260-414d-9fe4-7c74dd014285] Exception type: Microsoft.Identity.Client.MsalUiRequiredException
, ErrorCode: user_null
HTTP StatusCode 0
CorrelationId
at Microsoft.Identity.Client.Internal.Requests.Silent.SilentRequest.ExecuteAsync(CancellationToken cancellationToken)
at Microsoft.Identity.Client.Internal.Requests.Silent.SilentRequest.ExecuteAsync(CancellationToken cancellationToken)
at Microsoft.Identity.Client.Internal.Requests.RequestBase.RunAsync(CancellationToken cancellationToken)
[INF] Microsoft.Identity.Web.TokenAcquisition: [MsIdWeb] An error occured during token acquisition: "No account or login hint was passed to the AcquireTokenSilent call. "
MSAL.NetCore.4.36.0.0.MsalUiRequiredException:
ErrorCode: user_null
Microsoft.Identity.Client.MsalUiRequiredException: No account or login hint was passed to the AcquireTokenSilent call.
at Microsoft.Identity.Client.Internal.Requests.Silent.SilentRequest.ExecuteAsync(CancellationToken cancellationToken)
at Microsoft.Identity.Client.Internal.Requests.Silent.SilentRequest.ExecuteAsync(CancellationToken cancellationToken)
at Microsoft.Identity.Client.Internal.Requests.RequestBase.RunAsync(CancellationToken cancellationToken)
at Microsoft.Identity.Client.ApiConfig.Executors.ClientApplicationBaseExecutor.ExecuteAsync(AcquireTokenCommonParameters commonParameters, AcquireTokenSilentParameters silentParameters, CancellationToken cancellationToken)
at Microsoft.Identity.Web.TokenAcquisition.GetAuthenticationResultForWebAppWithAccountFromCacheAsync(IConfidentialClientApplication application, ClaimsPrincipal claimsPrincipal, IEnumerable1 scopes, String authority, MergedOptions mergedOptions, String userFlow, TokenAcquisitionOptions tokenAcquisitionOptions) at Microsoft.Identity.Web.TokenAcquisition.GetAuthenticationResultForUserAsync(IEnumerable1 scopes, String authenticationScheme, String tenantId, String userFlow, ClaimsPrincipal user, TokenAcquisitionOptions tokenAcquisitionOptions)
StatusCode: 0
ResponseBody:
Headers:
[ERR] 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.
---> Microsoft.Identity.Web.MicrosoftIdentityWebChallengeUserException: IDW10502: An MsalUiRequiredException was thrown due to a challenge for the user. See https://aka.ms/ms-id-web/ca_incremental-consent.
---> MSAL.NetCore.4.36.0.0.MsalUiRequiredException:
ErrorCode: user_null
Microsoft.Identity.Client.MsalUiRequiredException: No account or login hint was passed to the AcquireTokenSilent call.
at Microsoft.Identity.Client.Internal.Requests.Silent.SilentRequest.ExecuteAsync(CancellationToken cancellationToken)
at Microsoft.Identity.Client.Internal.Requests.Silent.SilentRequest.ExecuteAsync(CancellationToken cancellationToken)
at Microsoft.Identity.Client.Internal.Requests.RequestBase.RunAsync(CancellationToken cancellationToken)
at Microsoft.Identity.Client.ApiConfig.Executors.ClientApplicationBaseExecutor.ExecuteAsync(AcquireTokenCommonParameters commonParameters, AcquireTokenSilentParameters silentParameters, CancellationToken cancellationToken)
at Microsoft.Identity.Web.TokenAcquisition.GetAuthenticationResultForWebAppWithAccountFromCacheAsync(IConfidentialClientApplication application, ClaimsPrincipal claimsPrincipal, IEnumerable1 scopes, String authority, MergedOptions mergedOptions, String userFlow, TokenAcquisitionOptions tokenAcquisitionOptions) at Microsoft.Identity.Web.TokenAcquisition.GetAuthenticationResultForUserAsync(IEnumerable1 scopes, String authenticationScheme, String tenantId, String userFlow, ClaimsPrincipal user, TokenAcquisitionOptions tokenAcquisitionOptions)
StatusCode: 0
ResponseBody:
Headers:
--- End of inner exception stack trace ---
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)
Activity
jennyf19 commentedon Oct 20, 2021
JFercan Can you provide your Startup.cs?
JFercan commentedon Oct 23, 2021
Startup.txt
Hello jennyf19 ,
As requested please find attached a version of our Startup.cs file.
Thank you,
JFercan commentedon Feb 17, 2022
Hello jennyf19
Is there any update on this issue? It is still happening even after updating to version 1.23.0 for both Microsoft.Identity.Web and Microsoft.Identity.Web.MicrosfotGraph.
With this new version I still experience the same issue as 1.16.1.
Thank you.
jennyf19 commentedon Feb 19, 2022
JFercan I would need to see the code of the controller, but you might need to pass the AuthenticationScheme if it's different from the default authentication scheme:
Also, what does this do:
could it be overriding the default auth scheme?
JFercan commentedon Feb 19, 2022
Hello jennyf19 ,
I tried adding WithAuthenticationScheme but it does not seem to be an option on Microsoft.Graph version 4.11.0. Some devices authenticate using a key instead of Azure AD, and this is a customer handler for those. We might move this to use a Secret from the Azure AD registration instead.
I did get Graph to work by creating the instance this way, not sure if it would help:
Thank you.
jennyf19 commentedon Feb 20, 2022
JFercan
WithAuthenticationSchemeis added with Microsoft.Identity.Web.MicrosoftGraph, it's an extension method.JFercan commentedon Feb 21, 2022
Hello jennyf19 ,
Tried changing the code as follows:
However I still got the following exception:
Let me know if more details are required.
Thank you,
jmprieur commentedon Mar 28, 2022
See PR: #1672
jennyf19 commentedon Apr 20, 2022
Released in 1.24.0.
MattHartz commentedon Oct 14, 2022
So what is the solution to this jennyf19 Jean-Marc Prieur (@jmprieur)
jmprieur commentedon Oct 14, 2022
This was fixed in a later version of Id.Web