Share via

Blazor WASM encountering issues refreshing Access Token

Csincsi Botond 20 Reputation points
2023-11-09T10:11:39.8566667+00:00

I am encountering an issue where my Blazor WebAssembly (WASM) application can initially access my access token with no problems, but after a few hours, it cannot refresh the token on mobile devices, whereas on my PC, it still works fine. The AccessToken result on mobile returns false, which I assume indicates that the website failed to get a new access token. It starts working again once I log out and log back in.

Here's my dependency injection:

builder.Services.AddHttpClient("WebAPI",
    client => client.BaseAddress = new Uri(RestApiConnection.AttendanceURL))
    .AddHttpMessageHandler<CustomAuthorizationMessageHandler>();

builder.Services.AddTransient<CustomAuthorizationMessageHandler>();

builder.Services.AddMsalAuthentication(options =>
            {
                builder.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
                options.ProviderOptions.DefaultAccessTokenScopes.Add("api://custom/access_as_user");
                options.ProviderOptions.Cache.CacheLocation = "localStorage";                       
            });

This is where the exception is being thrown:

protected override async Task OnInitializedAsync()
{        
    var tokenResult = await TokenProvider.RequestAccessToken();        
    Console.WriteLine($"Access Result: {tokenResult?.TryGetToken(out Token)}");
    Console.WriteLine($"Access Token: {Token?.Value}");

And the issue is that the Access Result is false.

Developer technologies | .NET | Blazor
Microsoft Security | Microsoft Entra | Microsoft Entra ID
Microsoft Security | Microsoft Authenticator
Microsoft Security | Microsoft Identity Manager
Developer technologies | ASP.NET Core | Other

Answer accepted by question author

Bruce (SqlWork.com) 84,086 Reputation points
2023-11-14T17:41:21.8133333+00:00

in Apple's on-going battle against user tracking it has placed restrictions on local storage:

https://webkit.org/blog/10218/full-third-party-cookie-blocking-and-more/

you should use the browsers debug tools. Your Mac Safari can remote debug an IOS Safari app. Use Chrome for android. browsers.

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.