Skip to content

Blazor WASM Authentication - TryGetToken is null - Version 3.2.0-preview2.20160.5 #19998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
aletc1 opened this issue Mar 19, 2020 · 2 comments
Closed
Assignees
Labels
area-blazor Includes: Blazor, Razor Components Done This issue has been fixed feature-blazor-wasm This issue is related to and / or impacts Blazor WebAssembly

Comments

@aletc1
Copy link

aletc1 commented Mar 19, 2020

Describe the bug

Using MSAL (With Azure Active Directory), after a successful login (when calling AuthenticationService.RequestAccessToken, the method tokenResult.TryGetToken(out var token) returns a null token value.

To Reproduce

Just generate a new MSAL project using:

dotnet new blazorwasm -au SingleOrg --client-id "{CLIENT ID}" --tenant-id "{TENANT ID}"

And to reproduce, consider the following code in index.razor

@code {

    protected override async Task OnInitializedAsync()
    {
        var tokenResult = await AuthenticationService.RequestAccessToken();
        
        if (tokenResult.TryGetToken(out var token))
        {
            if (token == null)
                throw new Exception("Access token is null. BUG?");
        }
    }
}

token shouldnt be null, since was successfully authenticated.

@mkArtakMSFT mkArtakMSFT added area-blazor Includes: Blazor, Razor Components feature-blazor-wasm This issue is related to and / or impacts Blazor WebAssembly labels Mar 19, 2020
@miri10134
Copy link

miri10134 commented Mar 20, 2020

I was about to open an issue for the same problem. On trying to understand the source of the problem I looked into the source code of the Interop/AuthenticationService-Class and found the following:

async getAccessToken(request?: AccessTokenRequestOptions): Promise<AccessTokenResult> {
        try {
            const newToken = await this.getTokenCore(request?.scopes);

            return {
                status: AccessTokenResultStatus.Success,
                token: newToken
            };

        } catch (e) {
            return {
                status: AccessTokenResultStatus.RequiresRedirect
            };
        }
    }

    async getTokenCore(scopes?: string[]): Promise<AccessToken | undefined> {
        const tokenScopes = {
            redirectUri: this._settings.auth.redirectUri as string,
            scopes: scopes || this._settings.defaultAccessTokenScopes
        };

        try {
            const response = await this._msalApplication.acquireTokenSilent(tokenScopes);
            return {
                value: response.accessToken,
                grantedScopes: response.scopes,
                expires: response.expiresOn
            };
        } catch (e) {
            return undefined;
        }
    }

getTokenCore catches every exception and just returns undefined, which is not checked in getAccessToken. This leads to a result where "status" is set to "Success", but "token" is empty. The outer catch-block (in getAccessToken) will probalby never be reached.

This does not explain the actual error but it is something that should be addressed along the way.

Edit:
The getAccessToken-Method in WebAssembly.Authentication looks better and does basically the same except for the underlying _userManager instead of _msalApplication. Maybe adopt this one to make them consistent?

@mkArtakMSFT mkArtakMSFT added this to the blazor-wasm-3.2-preview4 milestone Mar 23, 2020
@javiercn javiercn added Done This issue has been fixed and removed Working labels Mar 26, 2020
@MortenMeisler
Copy link

Holy moly this was driving me crazy!! Is this fixed in pv 4 shipping 6th Apr? Maybe obvious not sure 💯 Any workarounds untill then?

@ghost ghost locked as resolved and limited conversation to collaborators May 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components Done This issue has been fixed feature-blazor-wasm This issue is related to and / or impacts Blazor WebAssembly
Projects
None yet
Development

No branches or pull requests

5 participants