Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incompatible Blazor Web App and Blazor Server #1084

Open
augustevn opened this issue Jan 21, 2025 · 0 comments
Open

Incompatible Blazor Web App and Blazor Server #1084

augustevn opened this issue Jan 21, 2025 · 0 comments

Comments

@augustevn
Copy link

augustevn commented Jan 21, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant