Skip to content

[Blazor] Improve authentification and usage of web apis #61501

Open
@MarvinKlein1508

Description

@MarvinKlein1508

I haven't found any open issues regarding JWT and Blazor in general, so I might end up opening one myself.

This might be related to #38111. As well as to my previous issue #59433 which got closed in favor of #55307 (Which only covers WASM btw.)

Almost every web API uses JSON Web Tokens (JWT) to manage access for API endpoints. This often involves authentication being done on the web API itself, which then returns both an access and a refresh token to the client.

Since I have already authenticated the user on the web API, I want to use these tokens to authenticate my user in my Blazor app as well. But now the topic gets really complicated.

First off, the official docs themselves do not contain any information about JWT in Blazor at all. I've opened another issue regarding this in the docs repository: dotnet/AspNetCore.Docs#35225

OK, when there are no official docs from Microsoft, let's search the web! Surely someone was smart enough to figure out how this works! But it turns out that it is extremely difficult to find anything good related to this topic—especially when you only consider Blazor Server, not SSR or WebAssembly.

I've literally seen everything here:

  • Custom cookie scripts to set cookies from interactive server mode
  • Storing both the access token and refresh token in local storage and manually applying those values every time you want to call the API
  • Ignoring refresh tokens altogether and simply providing an access token that expires in x months
  • Saving the access token as a custom cookie and the refresh token in local storage (to fetch JWT from a custom AuthenticationStateProvider)
  • Saving the JWT as a default claim in standard cookie authentication (often used with long-lived access tokens, since those cookies cannot be changed easily—especially with all the different render modes)
  • Storing XML files on the server itself, which are read using the user ID and a custom AuthenticationStateProvider (Blazor Server and SSR)

I may not be the smartest person when it comes to authentication, but even I can see that there are major security penalties in each of these described "solutions."

Storing this information is only the first part of the story. It gets even more complex when you want to automatically refresh the access token and apply it to all requests from your Blazor app. Depending on your chosen hacky method from above, you either apply those from localStorage or cookies manually for each request. This also isn't 100% reliable, since you cannot access JSInterop in all render modes from Blazor.

Blazor is a really cool framework, but the authentication part is really hard to overcome—especially for newcomers. I strongly believe that Blazor could be so much more popular if these issues were addressed. Authentication has been driving me insane since .NET 3.1. Up to this date, I still haven’t figured out how to refresh the user's claims on the fly without logging out and back in.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocsThis issue tracks updating documentationarea-security

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions