Skip to main content

Blazor Wasm - Get Access Token for User

05 Sep 2020 - Cody Merritt Anhorn

In this article I show, using ASP.NET Core Blazor Wasm, a quick snippet to get the AccessToken for a logged in User. Not much to it just using the IAccessTokenProvider, and if the user is signed in and they have are using an authentication type that provides an access token, like OpenID.

Checkout the Blazor Server - Get Access Token for User article with details on how this can be done for a Blazor Server Application!

Blazor - Getting Access Token from IAccessTokenProvider


@page "/"



using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;

public partial class Index : ComponentBase
    IAccessTokenProvider TokenProvider { get; set; }

    public string AccessToken { get; set; }

    protected override async Task OnInitializedAsync()
        var accessTokenResult = await TokenProvider.RequestAccessToken();
        AccessToken = string.Empty;

        if (accessTokenResult.TryGetToken(out var token))
            AccessToken = token.Value;

Categories: blog blazor

Tags: Blazor .NET C# Wasm

Cody Merritt Anhorn