How to use a blazor server app with a web api for authentication?

António Albuquerque 5 Reputation points
2023-11-30T19:45:34.3966667+00:00

I've search the web for a simple tutorial for this but I can't seem to find this exact scenario.

I have the following projects:

WebApi This is a standard .NET 8.0 web api with individual accounts authentication. I've created everything using the microsoft defaults and I'm using the new endpoints created automatically by app.MapGroup("/account").MapIdentityApi(). This works fine and I'm able to login and register using postman / swagger

ApiClient

This is a class library that handles all the calls to the WebApi including the login, register, etc. This ApiClient library has a method to set the access token before calling the actual method if it required authentication. This is also working fine

Now, what I want to do now is to create a Blazor Server app (I'm new at Blazor) that will provide me an UI for Login (for example) and when the user puts in the username/password it will call the apiClient.Login(...) method with the username and password. What I need to do then is check if I get the correct token and if so store this somewhere on blazor side and tell blazor that I'm authenticated. So when I go to an [Authorized] page in blazor and it requires me to call an api, it will pass the access token stored on login and call the apiClient method I want.

All examples online either use a different authentication JWT token, AAD, etc. or they simple show creating the database and the auth layer all in the Blazor server side. This is not what I need. I need a standalone API using the new IdentityUI endpoints and a way to connect from blazor to that API and keep me logged in, etc.

I think I need to create a custom AuthenticationStateProvider but I still don't understand how can I handle this. Do I use cookie based auth in blazor? Do I rely solely on the api to know I'm logged in? How will the AuthorizedViews in blazor know that I'm authenticated. I assume I have to manually set the user in the HttpContext.User with the claims I get from the API, etc.

Another thing is the 2FA authentication. The generated identity UI endpoint for the login works as follows: You login with your username / password, you get an 401 error back with the detail "TwoFactorRequired" and you then need to call the same login endpoint with the user / pass and 2fa code. Which means that in the blazor UI I would need to catch that 401, keep the user/pass in a hidden or variable, show the 2FA code page and then call login again with the 3 values. This doesn't seem too secure if I'm storing the username/password in a hidden field just to get the 2FA. Or maybe there is another way to login with 2fa without using the standard UI endpoint.

Can someone point me to a tutorial or give me some lights on how to implement this?

My reason to want to use a independent API is because I want to be able to expose the said API to other devices, etc.

The reason I want to use the default identity UI endpoints is for the management of all the 2FA, recovery codes, refresh tokens, etc. I had a custom logic using JWT tokens in the past and it was a pain to maintain.

Any help is appreciated.

Thanks

Developer technologies | .NET | Blazor

1 answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    1 deleted comment

    Comments have been turned off. Learn more

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.