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.

[Bug] Crashing Microsoft.Identity.Client.Broker - WithBrokerPreview()

See original GitHub issue

Logs and network traces

During logout:
Ausnahme ausgelöst: "Microsoft.Identity.Client.NativeInterop.MsalRuntimeException" in Microsoft.Identity.Client.NativeInterop.dll
GlobalExceptionHandler: Status: ApiContractViolation
Context: (pii)
Tag: 0x2039c1cb 
Ausnahme ausgelöst: "Microsoft.Identity.Client.NativeInterop.MsalRuntimeException" in Microsoft.Identity.Client.NativeInterop.dll
GlobalExceptionHandler: Status: ApiContractViolation
Context: (pii)
Tag: 0x2039c1cb 
During login:

no logs, app close without any error in VisualStudio. But not always (~70% crash), sometimes login is working.

Which version of MSAL.NET are you using? Microsoft.Identity.Client 4.46.2

Platform .NET 6 WPF

What authentication flow has the issue?

  • Desktop / Mobile
    • Interactive
    • Integrated Windows Authentication
    • Username Password
    • Device code flow (browserless)
  • Web app
    • Authorization code
    • On-Behalf-Of
  • Daemon app
    • Service to Service calls

Is this a new or existing app? This is a new app or experiment.

Repro

readonly string tenantId = "common";
private static string Instance = "https://login.microsoftonline.com/";
readonly string clientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
private IPublicClientApplication? _clientApp;
private IPublicClientApplication? PublicClientApp { get { return _clientApp; } }
private static AuthenticationResult? authResult;

...

_clientApp = PublicClientApplicationBuilder.Create(clientId)
                    .WithAuthority($"{Instance}{tenantId}")
                    .WithRedirectUri("http://localhost")
                    .WithBrokerPreview()
                    .Build();

...
authResult = await PublicClientApp.AcquireTokenInteractive(scopes)
                        .WithParentActivityOrWindow(new WindowInteropHelper(window).Handle)
                        .WithUseEmbeddedWebView(true)
                        .ExecuteAsync();
...

//logout
IEnumerable<IAccount> accounts = await PublicClientApp.GetAccountsAsync();
                if (accounts.Any())
                {
                    try
                    {
                        await PublicClientApp.RemoveAsync(accounts.First());
                    }
                    catch (MsalException ex)
                    {
                        Trace.WriteLine($"MsalException Error signing-out user: {ex.Message}");
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine($"Exception Error signing-out user: {ex.Message}");
                    }
                }

Expected behavior No crashing app, no errors on logout.

Actual behavior During login, the app is crashing very often, during logout I get errors as above written

Possible solution not using WithBrokerPreview(), instead see alternative code.

Alternative working code same as above, just remove

.WithBrokerPreview()

and you receive the “old” Loginscreen from Microsoft.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
gladjohncommented, Sep 9, 2022

Hi @pwallner

for the original issue that you see,

GlobalExceptionHandler: Status: ApiContractViolation Context: (pii) Tag: 0x2039c1cb

I am able to repro this when no scopes is passed, this works with the old WAM experience but not the Preview. We will have a fix for this in the next MSAL release

1reaction
pwallnercommented, Sep 9, 2022

Hi @gladjohn

This explains # 1 as I read, but not sure if it explains all other points. For now I use the broker (without preview), and there are no errors.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Bug] Crashing Microsoft.Identity.Client.Broker ...
NativeInterop.MsalRuntimeException" in Microsoft.Identity.Client. ... not using WithBrokerPreview() , instead see alternative code.
Read more >
Desktop app that calls web APIs: Acquire a token by using ...
MSAL falls back to a browser. WAM integration package. Most apps need to reference the Microsoft.Identity.Client.Broker package to use this ...
Read more >
Untitled
2022 · pwallner commented on December 22, 2022 [Bug] Crashing Microsoft.Identity.Client.Broker - WithBrokerPreview(). from microsoft-authentication ...
Read more >
Microsoft.Identity.Client fails in .Net Core app
Now, I'm trying to write the same but in a .Net Core console app. It crashes at the third line with error: Error...
Read more >
Microsoft.Identity.Client.Broker 4.55.0
This package contains binaries needed to use brokers in public client desktop applications and supersedes Microsoft.Identity.Client.Desktop.
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