question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IPublicClientApplication.AcquireTokenInteractive API throws NullReferenceException

See original GitHub issue

Which Version of MSAL are you using ?

v4 of Microsoft.Identity.Client

Platform .net 4.7

What authentication flow has the issue?

  • Desktop / Mobile
    • Interactive

Other? - please describe;

Is this a new or existing app? New app

Repro

// create public client using WithAdfsAuthority IPublicClientApplication _app = PublicClientApplicationBuilder.Create(ClientId) .WithAdfsAuthority(“<>”, false) .Build();

// Authenticate the user interactive AuthenticationResult result =await _app.AcquireTokenInteractive(Scopes).ExecuteAsync().ConfigureAwait(false);

// clear the cache var accounts = (await _app.GetAccountsAsync()).ToList(); while (accounts.Any()) { await _app.RemoveAsync(accounts.First()); accounts = (await _app.GetAccountsAsync()).ToList(); }

// try to authenticate the user again: AuthenticationResult result =await _app.AcquireTokenInteractive(Scopes).ExecuteAsync().ConfigureAwait(false);

Expected behavior User is asked for creds and authenticated

Actual behavior That API throws NullReferenceException with the following callstack.

at Microsoft.Identity.Client.Instance.AdfsUpnHelper.GetDomainFromUpn(String upn) at Microsoft.Identity.Client.Instance.AuthorityEndpointResolutionManager.TryGetCacheValue(AuthorityInfo authorityInfo, String userPrincipalName, AuthorityEndpoints& endpoints) at Microsoft.Identity.Client.Instance.AuthorityEndpointResolutionManager.<ResolveEndpointsAsync>d__3.MoveNext() — End of stack trace from previous location where exception was thrown — at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Identity.Client.Internal.Requests.RequestBase.<ResolveAuthorityEndpointsAsync>d__20.MoveNext() — End of stack trace from previous location where exception was thrown — at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Identity.Client.Internal.Requests.InteractiveRequest.<ExecuteAsync>d__9.MoveNext() — End of stack trace from previous location where exception was thrown — at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Identity.Client.Internal.Requests.RequestBase.<RunAsync>d__14.MoveNext() — End of stack trace from previous location where exception was thrown — at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Identity.Client.ApiConfig.Executors.PublicClientExecutor.<ExecuteAsync>d__2.MoveNext() — End of stack trace from previous location where exception was thrown — at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult() at TodoListClient.MainWindow.<SignIn>d__18.MoveNext() in H:\oauthExamples\active-directory-dotnet-native-desktop\TodoListClient\MainWindow.xaml.cs:line 332

Possible Solution Handle UPN being not present

Additional context/ Logs / Screenshots Add any other context about the problem here, such as logs and screebshots. Logging is described at https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/wiki/logging

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
jmprieurcommented, Jul 2, 2019

@saketkataruka @fdbeirao : the fixe is now available in MSAL.NET 4.1 cc: @jennyf19

1reaction
fdbeiraocommented, Jul 2, 2019

Hey @jennyf19 . I don’t think there was something you could have done. ADFS is a quite complex beast, and when something goes wrong it’s always going to be difficult. Together with a “random” NRE, it becomes slightly harder even.

I was providing consulting for a friend who is using this library. Even after downloading the debug symbols, the NRE was still in a somewhat unrelated place (null.Contains("@")). Most of the struggle was with Visual Studio, for losing bits and pieces of the call stack due to the usage of async/awaits. It was quite a stroke of luck that we found this Issue, after searching for “TryGetCacheValue” here on GitHub.

Also, the “bug” was also only triggered after the second login to the system, because the second execution of TryGetCacheValue would have a value in the cache, but the first didn’t (so it returned false immediately). 🤷🏻‍♂️

Thank you for the fix and the follow up 🙏🏻

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to resolve "possible System.NullReferenceException" ...
I am following the sample code provided in "Azure-Sample" to acquire token to call Microsoft Graph Api. But Resharper suggesting "Possible ...
Read more >
Unable to extract Regional and Language Settings ...
Unable to extract Regional and Language Settings from Graph API Beta ... but it throwing "Null" values in my local c# application.
Read more >
Using web browsers (MSAL.NET) - Microsoft Entra
Opening the browser with sudo is unsupported by MSAL and will cause MSAL to throw an exception. On macOS, the browser is opened...
Read more >
netFramework/Microsoft.Identity.Client.xml 2.0.5-Preview1
the httpClient had the possibility of throwing an exception stating "Properties can ... IPublicClientApplication"/> call <c>AcquireTokenInteractive</c> so
Read more >
Issues with System.NullReferenceException occurrence ...
NET Core 3.1 is breaking when starting the web application because of a NullReferenceException, but this one is thrown in the IISHttpServer.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found