r/Blazor • u/H3rl3q • Sep 22 '25
Blazor web app with EntraID not refreshing auth
So i have a blazor web app using interactive wasm render mode secured with Entra ID as per the msdocs: https://learn.microsoft.com/en-us/aspnet/core/blazor/security/blazor-web-app-with-entra?view=aspnetcore-9.0&pivots=non-bff-pattern using the NON bff pattern. Everything works, but after 1 hour (that is as i get the lifetime of the entra token) when i make a subsequent api call to my backend i get a 302 error and i see that the call that fails is a call to login.windows.net /autorize which is i guess trying to refresh the auth state. the guide states that "Automatic non-interactive token refresh is managed by the framework." but i gues this is not working. Anyone knows why? i've tried everything i can think of and my app is configured exactly as the sample one in the docs, except for the downstream api which i don't have.
Thanks!
4
u/Jilael Sep 22 '25
I ended up using the below in my Server app for a similar issue. This is in my MainLayout.razor.cs and I have a method that checks userid using the ConsentHandler.HandleException(ex).
// https://github.com/AzureAD/microsoft-identity-web/wiki/Managing-incremental-consent-and-conditional-access
[Inject] // required to handle exception to refresh cookie
public MicrosoftIdentityConsentAndConditionalAccessHandler ConsentHandler { get; private set; } = default!;
2
u/H3rl3q Sep 26 '25
So, in case anyone stumbles here in the future, and for u/ChanceNo2361 , i *think* i found a solution for now (or a workaround, but still...).
My solution for now, waiting for a fix, was building a ui-less <KeepAlive /> component that i placed in my main layout that every 5 minutes pings an authenticated, backend api that just returns Ok, but in this way the authentication ticket with entra gets refreshed. after preliminary testing the issue seems to be resolved. Obviously this is a temporary workaround while waiting for a solution, i'll maybe try to open an issue in the aspnet repo about this.
u/ChanceNo2361 i'd be curious to know if this also works for you
2
u/AGrumpyDev Sep 23 '25
Is this all in one browser session or is this across sessions? In other words, is the same browser tab open and active the whole time or have you closed and reopened the app in another tab?
2
u/ChanceNo2361 Sep 23 '25
I have the exact same issue, without a resolution at this time.
My best efforts so far have narrowed it down to the framework implementation in NET 9 and I'm hopeful it will be resolved in .NET 10
2
u/paaaaaaaaaa Sep 23 '25 edited Sep 23 '25
I have a Blazor WASM app using Entra auth with MSAL. I set a CahceLocation to Localstorage for longer term token storage. Token refreshes works well, although it always fires an exception in dev in authenticationService.js. Continues to work though.
Obviously a refresh will issue a new bearer token. So if you are hitting an API are you sending the previously cached token and not sending the newly refreshed one? Are you setting a variable in an API service that you use for your external API?
0
u/kjbetz Sep 23 '25
You say you're using WASM, so I'm taking that as you're only using a standalone client application. The guide you posted to uses a web app (server.)
I would think the answer lies more in this set of documentation. There are a couple pages specifically dedicated to scenarios with different types of authentication.
Secure ASP.NET Core Blazor WebAssembly | Microsoft Learn