aspnetcore: MSAL : Azure B2C Blazor wasm fails after 24 hours when the refresh token has been expired

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

We are using Azure B2C login with a Blazor wasm application. Using local storage to save the tokens and the goal is not to ask user credentials again if the user tries to access the same site till the refresh token expires. This works well.

But we are facing an issue if we try to access the application after a period of 24 hours of inactivity. This is the time when the refresh token expires(SPA with PKEC in azure B2C has 24 hour expiry for refresh token). After 24 hours the application is trying to login the user through a hidden iframe and for some security reason some of the browsers are throwing a warning and the login process fails.

I can see below message in the console An iframe which has both allow-scripts and allow-same-origin for its sandbox attribute can escape its sandboxing.

I need to know is there any way to fix this. Does MSAL in Blazor provides any customization to disable this hidden iframe and enable the normal browser redirect?

Here is my configuration in program.cs

builder.Services.AddMsalAuthentication(options =>
{
    options.ProviderOptions.LoginMode = "redirect";
    builder.Configuration.Bind("AzureAdB2C", options.ProviderOptions.Authentication);
    options.ProviderOptions.DefaultAccessTokenScopes.Add("SomeScope");
    options.ProviderOptions.Cache.StoreAuthStateInCookie = true;
    options.ProviderOptions.Cache.CacheLocation = "localStorage";
    options.UserOptions.RoleClaim = ClaimTypes.Role;
});

Here are some screenshots from the browser console which can help to understand the issue better.

In case if someone needs to re-create this issue without waiting for 24 hours, then you can replace the access token and refresh token to an already expired value and try this in Chrome Incognito or Mozilla. In normal chrome I am not getting this issue, but some other are getting the issue even in non incognito chrome window.

image

image

Expected Behavior

The application should have with some kind of a fallback strategy. Let say if the browser doesn’t support hidden iframe redirect, then define a fallback mechanism to do a regular browser redirect.

Steps To Reproduce

Login to Azure B2C Use local storage to store the token cache in client side Try to load the application again after 24 hours(ie after the refresh token expires) See the browser logs

Note: In case if someone needs to re-create this issue without waiting for 24 hours, then you can replace the access token and refresh token to an already expired value and try this in Chrome Incognito or Mozilla. In normal chrome I am not getting this issue, but some other are getting the issue even in non incognito chrome window.

Exceptions (if any)

In the chrome console windows we can see a warning like An iframe which has both allow-scripts and allow-same-origin for its sandbox attribute can escape its sandboxing.

In the redirect response we can see an error like error=interaction_required&error_description= AADB2C90077 User does not have an existing session and request prompt parameter has a value of None

.NET Version

7

Anything else?

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 26 (4 by maintainers)

Most upvoted comments

Hi @halter73 I do understand that there is a similar issue logged 12 days before, but you have closed that issue also without any resolution. Its very un-fortunate that you people are not having a solution even after an year. Please show this issue to someone who developed this AuthenticationService.ts, they can fix this quickly.

I understand that browser blocks the 3rd party cookie and the silent token renew fails. so what is the ideal solutions? We need a configuration in this js pluggin so that we can disable the silent renewal of token which is the issue here. Or if the silent renewal fails you have to automatically do the full redirect for getting the new token. As a framework I would except this to be done by the AuthenticationService.ts rather than each developer doing their own fix for the same problem.

@halter73 Instead of closing the issue blindly please review this issue correctly and give us a proper fix and we are waiting for more than an year.

This appears to be a dupe of https://github.com/dotnet/aspnetcore/issues/48264 that got lost in the servicing milestone. 24 hours is the documented refresh token lifetimefor single page apps since the tokens are accessible from JS in the browser unlike traditional server-side apps which have a 90-day lifetime.

Refresh tokens sent to a redirect URI registered as spa expire after 24 hours. Additional refresh tokens acquired using the initial refresh token carry over that expiration time, so apps must be prepared to rerun the authorization code flow using an interactive authentication to get a new refresh token every 24 hours. Users don’t have to enter their credentials and usually don’t even see any related user experience, just a reload of your application. The browser must visit the sign-in page in a top-level frame to show the login session. This is due to privacy features in browsers that block third party cookies.

https://learn.microsoft.com/en-us/entra/identity-platform/refresh-tokens#token-lifetime

The main regression here is that browsers have started to block third-party cookies by default even in iframes meaning a full-page navigation is required to refresh the token. Unlike a silent iframe-based refresh, Blazor cannot do a full-page navigation automatically to refresh the token because it would be too breaking to automatically force full reload of the page which would lose application state.

As many people on this issue have already helpfully noted, the application needs to call AccessTokenNotAvailableException.Redirect() or NavigationManager.NavigateToLogin(accessTokenResult.InteractiveRequestUrl) to handle AccessTokenResultStatus.RequiresRedirect just like it did before browsers started blocking third-party cookies in iframes. It’s just that now that these cookies are blocked, a full-page redirect is more often needed even when the user does not need to reenter credentials or otherwise interact with Azure B2C which could have previously been done silently in an iframe without reloading the app. Fortunately, as noted in the refresh token docs, “users don’t have to enter their credentials and usually don’t even see any related user experience, just a reload of your application.”

https://github.com/dotnet/aspnetcore/issues/48264#issuecomment-2038558878 provides a more in-depth explanation of what’s going on, and it links to a full repro project that demonstrates both AccessTokenNotAvailableException.Redirect() or NavigationManager.NavigateToLogin(accessTokenResult.InteractiveRequestUrl).

If someone can provide a full repro project on GitHub where the AccessTokenResultStatus is incorrect, or where AccessTokenNotAvailableException.Redirect()/NavigationManager.NavigateToLogin(accessTokenResult.InteractiveRequestUrl) does not work, please file a new issue with a link to the repro.

Hi @hectormmg,

We are using blazor and could you please let me know how exactly this can be configured?

Thanks

We’ve moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

#48264 (comment) provides a more in-depth explanation of what’s going on, and it links to a full repro project that demonstrates both AccessTokenNotAvailableException.Redirect() or NavigationManager.NavigateToLogin(accessTokenResult.InteractiveRequestUrl).

The provided resolution https://github.com/halter73/BlazorHostedWasmAAD/compare/custom-delegating-handler essentially behave the same as MSAL sessionStorage.

localStorage should support long login session up to 3 months in B2C … What’s the benefit of enabling localStorage if users need a full redirect every 24 hours, essentially the same as sessionStorage 😦

Just thought I’d come back and update you with a workaround. The issues arises when calling an API with an expired access token. We have an implementation of AuthorizationMessageHandler as we want to the access token to outgoing requests for a particular API. Overriding the SendAsync method allows us to catch the AccessTokenNotAvailableException and call its Redirect method, which redirects the user as opposed to using the hidden iframe. Not sure why this isn’t done for you already and why you have to step in and call the Redirect method manually, but it solves our problem.

That doesn’t work

This worked for me pre .NET 8, after upgrading to .NET 8 this doesn’t work any more.

You get correctly redirected to the login page (‘authentication/login’), but I think that page also gets a ‘AccessTokenNotAvailableException’. So the page is just stuck.

Users are now stuck on an error page, and even a full refresh doesn’t solve this. Need to clear the site data completely.

I now get the following error: crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: net_http_message_not_success_statuscode_reason, 302, Token expired, redirecting to login.

Just thought I’d come back and update you with a workaround.

The issues arises when calling an API with an expired access token. We have an implementation of AuthorizationMessageHandler as we want to the access token to outgoing requests for a particular API.

Overriding the SendAsync method allows us to catch the AccessTokenNotAvailableException and call its Redirect method, which redirects the user as opposed to using the hidden iframe.

Not sure why this isn’t done for you already and why you have to step in and call the Redirect method manually, but it solves our problem.

Any updates on this please?

We’re also facing the same issue when the token expires.

Unfortunately, this bug is still here and it’s affecting our SPA which is running on .NET 8.0. I’m starting to think that I should really have gone with vuejs instead.

Discussed this with current @emlauber (her team owns MSAL.js). @emlauber feel free to unassign yourself if you find out that this is caused by Blazor, rather than MSAL.js (which is the current thinking). Thanks!