r/dotnet • • 5d ago

Promotion Learning to Implement JWT in .NET as a Beginner

Hey guys! I’m a beginner to web and .NET development, and here I share my learnings about JWT and how I implemented it in my .NET application.

I talked about access and refresh tokens, token rotation and revocation, and storage on both the client and the server.

You can read about it in more detail here:

https://paoloragudo.com/blog/posts/learning-to-implement-jwt-in-net

Questions, comments, and suggestions are welcome!

No AI has been used in writing the article

Edit: The expense tracker API part got less focus on, so I might have skipped implementation on a few things

Edit 2: Wow, I didn't expect this to be received well 😭. Glad everyone found it refreshing and nice!

As a beginner, I'm always afraid to push out things like this for fear that I got things wrong or be criticized, but I'm surprised by the positive feedback. Thank you guys!

54 Upvotes

24 comments sorted by

24

u/themode7 5d ago

refreshing to see blogs and experimental learning like this,

8

u/orbit99za 5d ago

This,No AI cheat,just like the old days, Read, Research, and stack overflow

4

u/IceSpikes_ 5d ago

Thank you so much! I don't deny the use of AI, but I try to minimize its use as much as possible in my learnings.Ā 

I'm also a big fan of blogs and informative stuff out there written by humans. I use AI for development and for learnings, but no AI for writing the blogs. It's all my own voice!

1

u/IceSpikes_ 5d ago

Thank you so much! I really appreciate it šŸ¤

1

u/Fhathyma_Putala 5d ago

Depends on the topic imo, experimental learning is awesome for general API stuff but custom JWT handling is usually where subtle security bugs sneak in

3

u/CmdrSausageSucker 5d ago

Very cool indeed, reminds me: 3 years ago I also implemented a hand-rolled jwt authentication solution (RBAC) for a .net app (Blazor wasm), with authentication via LDAP, mainly because I didn't know about Identity :-) It was interesting to say the least, the solution is still in use today.

2

u/IceSpikes_ 5d ago

Very cool! Are you still working with .NET applications today?

2

u/CmdrSausageSucker 2d ago

yes, absolutely :-)

3

u/Born-West9972 5d ago

Nice read went through ur most blogs, well written.

Added you to my rss feed.

2

u/IceSpikes_ 5d ago

Thank you so much!

2

u/_devpikachu 5d ago

Nice read! Thanks for this! Will be keeping a bookmark for your blog.

2

u/IceSpikes_ 5d ago

Thank you so much! Will try to write more as I go along my learning journey

2

u/You_CANnot_stop_me 5d ago

As someone who also just started working on .NET its very refreshing to see somebody willing to share their notes! Adding your blog yo my rss feed immediately.

2

u/IceSpikes_ 5d ago

Thank you! We're gonna keep going and improving! šŸ¤

2

u/chucker23n 5d ago

One note: I think you never explain what "signature" or "digitally signed" actually means, briefly. Something like "having a digital signature ensures anyone can verify the payload (and therefore, the entire token) hasn't been tampered with: changing the payload would invalidate the signature, and creating a new signature requires the private key, which you shouldn't share with anyone".

1

u/IceSpikes_ 5d ago

Ah right! Totally forgot. Thank you for the feedback! šŸ™‚ā€ā†•ļø

1

u/nitrogifter 5d ago

How do you write your blogs? Is it just md? And where do you host them?

2

u/IceSpikes_ 5d ago

Yes! They are just .mdx files and hosted on Vercel. The website is public on my github with a push to deploy setup

3

u/kagayaki 5d ago

Mostly well written article, but maybe I can criticize some of the practices.

First, a pedantic note -- maybe I'm misunderstanding what developers actually mean when they say "JWT" (vs. OAuth or OIDC), access and refresh tokens are not really part of the JWT spec at all. JWT is merely a token encoding standard that is primarily used for OAuth, and it is mostly accurate to say that OAuth defines access and refresh tokens as the two types of tokens since id tokens are part of the OIDC extension.

I also don't really understand the design choices. It's not really clear at least based on this single article how this hypothetical UI and the hypothetical API are integrating since I don't really see an entry point for how these Jwts would be generated for a user.

I'm also really confused about the idea of storing tokens as cookies that I assume are set/created by the API. This means all of your API calls have to be on the front channel and while it's good the tokens are HttpOnly.. I'm always nervous putting credentials in the browser.

Really the way that this should be done if anything is to have the JWT creation logic happen through the UI (assuming the UI actually has a backend). You would have whatever real authentication method your UI has create a cookie and then store the JWTs as AuthenticationTokens stored by cookie middleware on the frontend, similar to how it works with OIDC middleware. Then you should be able to use the HttpContext.GetTokenAsync() method to pull the authentication tokens when this API needs to be called by the UI. By default this would store that material in the encrypted cookie set by ASP.NET Core Cookie middleware, but at least it wouldn't be stored plain text.

To be fair though, I haven't had to handcraft a custom authentication handler yet since most of my coding is done at work where we have multiple enterprise authorization servers, so I just plug in OIDC with SaveTokens + Cookies if I need to use those access tokens and then I'm mostly done other than setting up authorization policies.

2

u/Variumcode 3d ago

Nice write up, honest and easy to follow. one thing to add on top of rotation if you haven't: reuse detection. if an old refresh token (already rotated) shows up again, someone probably stole it, so revoke the whole chain for that session, not just that token

1

u/AutoModerator 5d ago

Thanks for your post IceSpikes_. Please note that we don't allow spam, and we ask that you follow the rules available in the sidebar. We have a lot of commonly asked questions so if this post gets removed, please do a search and see if it's already been asked.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/EymenYildirim 1d ago

Good achievement bro. Next step I recommend you to go for the next chapter, know and use the security standards, like OIDC for web and mobile, OAuth2 for APIs and Backend for Frontend (BFF) to secure client side apps like Angular, react, Flutter, ...

I use OpenIdDict within my C# projects, it has many templates and starting points, however also you can use external identity providers like keycloak and Auth0.