Blazor Authentication with JSON Web Tokens

  Рет қаралды 7,968

Coding Tutorials

Coding Tutorials

11 ай бұрын

We've seen a ASP.NET backend with an Angular frontend. What if we want C# all the way through? Here's how we do it in Blazor.
Source code available at: github.com/JasperKent/Blazor-...
Server code available at: github.com/JasperKent/WebApi-...
Topics include:
- Creating HttpClient through an HttpClientFactory
- Using appsettings.json in Blazor WASM
- Accessing Session Storage with the Blazored.SessionStorage package
- Decomposing JWTs in .NET
- Using events to update the DOM in Blazor
- Using a DelegatingHandler to augment HTTP requests
- Avoiding cyclic dependency injection

Пікірлер: 28
@CodingTutorialsAreGo
@CodingTutorialsAreGo 11 ай бұрын
Which front end do you prefer - Angular or Blazor? Let me know. Source code available at: github.com/JasperKent/Blazor-Wasm-Authentication Server code available at: github.com/JasperKent/WebApi-Authentication Remember to subscribe at kzbin.info/door/qWQzlUDdllnLmtgfSgYTCA And if you liked the video, click the 👍.
@hollandrisley
@hollandrisley 5 ай бұрын
Great stuff, thanks! Just moving from Angular to Blazor so this helps alot :)
@smc_onetwo
@smc_onetwo 3 ай бұрын
huge help. i'll be implementing something like this. thank you
@syedhasnadjami6008
@syedhasnadjami6008 11 ай бұрын
Thanks for the video. I never knew that I can get the token from the class again. I always have written a custom parser for that. Thanks for showing that. Also l have a question. Instead of using DelegatingHandler for auth state, can't we use use the AuthenticationStateProvider for that as that also directly control the blazor AuthorizeView component
@CodingTutorialsAreGo
@CodingTutorialsAreGo 11 ай бұрын
Yep - that's for a future video.
@nicolasledesma1709
@nicolasledesma1709 8 ай бұрын
Thank you very much!!
@DimasMessias-kl4ic
@DimasMessias-kl4ic Ай бұрын
Where would you normally keep that JWT token? Keeping it in the session be easily found and decrypted to extract information from it. Is there a way to keep it only in memory completely invisible (using WASM)?
@CodingTutorialsAreGo
@CodingTutorialsAreGo Ай бұрын
It couldn't be decrypted, because the secret to decode it is kept on the server. Even if it could be decrypted, there's no sensitive information in there. The risk of having the JWT stolen is that it could be used to gain access to a secure server. Moreover, Session and Local storage are only available to the user of the machine, so are safe for a personal, password protected machine. On a public machine, however, they shouldn't be stored in Local storage, and the user should remember to close the browser to clear session storage. Storage in memory is safest, but least convenient.
@AthelstanEngland
@AthelstanEngland 8 ай бұрын
Thanks. This all makes sense to me, which in itself is a minor miracle!! Would there be a lot of difference if using Blazor Server rather than WASM?
@CodingTutorialsAreGo
@CodingTutorialsAreGo 8 ай бұрын
Yes, it would be quite different for Server. I'll try to get round to doing a video on that.
@AthelstanEngland
@AthelstanEngland 8 ай бұрын
@@CodingTutorialsAreGo thanks for the reply. I've just re-watched the Part 1 and Part 2 videos regarding securing the API itself. Very informative the way you added it to an existing project. If creating a new API and using one of the templates with Identity authentication/authorisation would that result in the same JWT token based system or is your approach different/enhanced/minimal compared to what MS gives you out of the box so to speak?
@CodingTutorialsAreGo
@CodingTutorialsAreGo 8 ай бұрын
@@AthelstanEngland If you go for Microsoft Identity Platform for the authentication, then it uses JWTs, but they are issued by Microsoft, not by you application itself as I've done here.
@AthelstanEngland
@AthelstanEngland 8 ай бұрын
@@CodingTutorialsAreGo hi and thanks for the confirmation. Yep, just this morning I decided to create a project using Identity Platform to see what happened and yep it wanted Azure login and all sorts. I deleted and went back to you videos!
@Arcadenut1
@Arcadenut1 7 ай бұрын
I am assuming that you're designing this for Client side and not Server side? If it's running server side you created your authorization service as a singleton, which from my understanding is global to ALL connected clients. Would that not mean the JWT token would be accessible to all the users since you are caching it in the Service?
@CodingTutorialsAreGo
@CodingTutorialsAreGo 7 ай бұрын
Absolutely. This is Blazor WASM only.
@PedalsizBerkay
@PedalsizBerkay 10 ай бұрын
Thanks
@waynehawkins654
@waynehawkins654 8 ай бұрын
Yes to download the code. Is this for .net 8 or .net 7?
@CodingTutorialsAreGo
@CodingTutorialsAreGo 8 ай бұрын
This one is 7.
@jerryjeremy4038
@jerryjeremy4038 3 ай бұрын
Is this the right way to do this?
@cissemy
@cissemy 11 ай бұрын
Thanks Can you add Refresh Token ? Thanks
@CodingTutorialsAreGo
@CodingTutorialsAreGo 11 ай бұрын
So many people asking for this. It's on the list
@cissemy
@cissemy 11 ай бұрын
Also Maui Blazor Authentication with JSON Web Tokens
@CodingTutorialsAreGo
@CodingTutorialsAreGo 11 ай бұрын
@@cissemy I'm not sure there's anything significantly different in MAUI Blazor than here.
@waynehawkins654
@waynehawkins654 8 ай бұрын
@@CodingTutorialsAreGo Yes please for refresh token.
Blazor and JWT Refresh Tokens
16:07
Coding Tutorials
Рет қаралды 3,6 М.
Storing Images in S3 from Node Server
39:59
Sam Meech-Ward
Рет қаралды 84 М.
WHO LAUGHS LAST LAUGHS BEST 😎 #comedy
00:18
HaHaWhat
Рет қаралды 22 МЛН
Cat Corn?! 🙀 #cat #cute #catlover
00:54
Stocat
Рет қаралды 15 МЛН
ЧУТЬ НЕ УТОНУЛ #shorts
00:27
Паша Осадчий
Рет қаралды 3,4 МЛН
Хотите поиграть в такую?😄
00:16
МЯТНАЯ ФАНТА
Рет қаралды 3,4 МЛН
Blazor Tutorial : Authentication | Custom AuthenticationStateProvider - EP12
29:46
CuriousDrive: Solve Coding Challenges. Win Prizes.
Рет қаралды 62 М.
8 Free and Open Source Blazor UI Libraries
9:42
Claudio Bernasconi
Рет қаралды 36 М.
What is JWT? JSON Web Tokens Explained (Java Brains)
14:53
Java Brains
Рет қаралды 1 МЛН
React and JWT
18:51
Coding Tutorials
Рет қаралды 546
Что не так с раскладушками? #samsung #fold
0:42
Не шарю!
Рет қаралды 182 М.
Top 50 Amazon Prime Day 2024 Deals 🤑 (Updated Hourly!!)
12:37
The Deal Guy
Рет қаралды 1,4 МЛН
ГОСЗАКУПОЧНЫЙ ПК за 10 тысяч рублей
36:28
Ремонтяш
Рет қаралды 424 М.
Собери ПК и Получи 10,000₽
1:00
build monsters
Рет қаралды 2,7 МЛН