dotnet-starter-kit icon indicating copy to clipboard operation
dotnet-starter-kit copied to clipboard

Incompatible Blazor Web App and Blazor Server

Open augustevn opened this issue 1 year ago • 1 comments

Hi,

A client asked me to upgrade a project started on the .NET 7 Blazor Wasm Fullstack Hero starter / template. An upgrade to .NET 8+ & to convert it to a Blazor Server app. Converting to a Blazor Server app or a Blazor Web App was troublesome due to its dependency on Microsoft.AspNetCore.Components.WebAssembly.Authentication and a major issue in the DelegatingHandler: JwtAuthenticationHeaderHandler.

On line 67 in the JwtAuthenticationService,cs, tokens are correctly stored into the local storage. Using the JsRuntime is no problem. However, line 70 triggers an HTTP call: var permissions = await _client.GetUserPermissionsAsync(); which in turn activates the DelegatingHandler: JwtAuthenticationHeaderHandler.

At this point, the DelegatingHandler attemtps to get an access token: _tokenProviderAccessor.TokenProvider.GetAccessTokenAsync() -> tokenProvider.RequestAccessToken(). Which crashes on a "statically rendered ... JsRuntime cannot be accessed ..." for some reason.

So, I tried filling properties on JwtAuthenticationService with those tokens and try to use those in the DelegatingHandler. Still NULL. Which makes me wonder if there's a scoping issue or rather Blazor Server's multi-threading nature or ... I'm just guessing. Anyway, my ugly workaround is to register an extra Singleton with properties for the tokens and the permissions. I simply inject that Singleton into the Delegating handler and that seems to work. I tried using the HttpContextAccessor as well but no cigar.

It would be great if you guys could drop the dependency on Microsoft.AspNetCore.Components.WebAssembly.Authentication and resolve the above scoping issue.

augustevn avatar Jan 21 '25 03:01 augustevn

I am planning to migrate it to BlazorWebApp soon. Not able to give you the right timeline now.

iammukeshm avatar Feb 22 '25 11:02 iammukeshm