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.

IsAuthenticated is always false for an acquired token in WPF

See original GitHub issue

Discussed in https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/discussions/3759

<div type='discussions-op-text'>

Originally posted by aoun-muhammad October 25, 2022 I am trying to implement Authentication for Blazor Hybrid (WPF) as per this documentation https://learn.microsoft.com/en-us/aspnet/core/blazor/hybrid/security/?view=aspnetcore-6.0&pivots=wpf

I have used the MSAL like this,

string[] scopes = new string[] { "user.read"};
AuthenticationResult authResult = await app.AcquireTokenInteractive(scopes)
            .WithAccount(firstAccount)
            .WithParentActivityOrWindow(new WindowInteropHelper(this).Handle) // optional, used to center the browser on the window
            .WithPrompt(Prompt.SelectAccount)
            .ExecuteAsync();

There is a ClaimsPrincipal property in authResult, which has ClaimsIdentity. But the problem is that it always has AuthenticationType as null and IsAuthenticated as false.

I would like to know why IsAuthenticated is always false, and what I can do so that I get it as true when I acquire a token.</div>

As per @bgavrilMS this is a bug, hence reporting it.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
bgavrilMScommented, Feb 10, 2023

Ack, let’s reconsider this.

1reaction
aoun-muhammadcommented, Feb 9, 2023

Disappointed to see this marked as closed - not planned. There’s an obvious disconnect between the Identity, ASP.NET/Blazor, and Maui teams.

For others looking for a workaround, this may help. I am using the Maui MSAL B2C sample here: https://github.com/Azure-Samples/ms-identity-dotnetcore-maui/blob/main/MauiAppB2C/MSALClient/MSALClientHelper.cs#L27

And I changed that line to recreate the AuthResult with a properly-formed Principal.

public AuthenticationResult AuthResult { get => _authResult; }
private AuthenticationResult _authResult;

/// <summary>
/// This is a workaround - result.ClaimsPrincipal.Identity.IsAuthenticated always returns false.
/// Microsoft.Identity.Client does not create a ClaimsPrincipal with that string parameter, authenticationType. 
/// So I copy everything over here, add that parameter, and set it. 
/// </summary>
/// <see href="https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/issues/3760"/>
/// <param name="result">result from the Microsoft.Identity.Client sign in methods</param>
public void SetAuthResult(AuthenticationResult result)
{
    var principal = new ClaimsPrincipal(new ClaimsIdentity(result.ClaimsPrincipal.Claims, "Bearer"));
    this._authResult = new AuthenticationResult(
            result.AccessToken,
            result.IsExtendedLifeTimeToken,
            result.UniqueId,
            result.ExpiresOn,
            result.ExtendedExpiresOn,
            result.TenantId,
            result.Account,
            result.IdToken,
            result.Scopes,
            result.CorrelationId,
            result.TokenType,
            result.AuthenticationResultMetadata,
            principal,
            result.SpaAuthCode
        );
}

Yes I have done something similar but I used “AuthenticationTypes.Federation” as authenticationType because this is what Blazor return. So for my WPF implementation I am using the same.

Read more comments on GitHub >

github_iconTop Results From Across the Web

IsAuthenticated is always false for an acquired token in WPF
I would like to know why IsAuthenticated is always false, and what I can do so that I get it as true when...
Read more >
User.Identity.IsAuthenticated always false in .net core when ...
I have an asp.net core application using json web tokens for authentication, this worked fine when my user Id was a string, ...
Read more >
Context.User.Identity.IsAuthenticated is always false in jwt ...
I am using signalr with jwt token. `services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.
Read more >
Desktop app that calls web APIs: Acquire a token interactively
Learn how to build a desktop app that calls web APIs to acquire a token for the app interactively.
Read more >
How to get an access token with Authorization Code Grant
To make an eSignature REST API request, you must include: The acquired access token in the request's Authorization header. The base URI for...
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