Adding JWT Authentication & Authorization in ASP.NET Core

Sdílet
Vložit
  • čas přidán 19. 04. 2023
  • Check out my courses: dometrain.com
    Become a Patreon and get source code access: / nickchapsas
    Hello, everybody, I'm Nick, and in this video, I will show you how to get started with JWT Authentication and Authorization in your ASP.NET Core applications and APIs.
    Video explaining JWT: • What is a JWT (JSON We...
    Workshops: bit.ly/nickworkshops
    Don't forget to comment, like and subscribe :)
    Social Media:
    Follow me on GitHub: bit.ly/ChapsasGitHub
    Follow me on Twitter: bit.ly/ChapsasTwitter
    Connect on LinkedIn: bit.ly/ChapsasLinkedIn
    Keep coding merch: keepcoding.shop
    #csharp #dotnet

Komentáře • 175

  • @nickchapsas
    @nickchapsas  Před rokem +127

    Just a heads up. Roles and advanced feature are intentionally left out of this video because I will do an advanced and best practices follow up video. Keep coding!

    • @maskettaman1488
      @maskettaman1488 Před rokem +7

      Looking forward to that one!

    • @jfpinero
      @jfpinero Před rokem +3

      You should do a RBAC vs ABAC video and their uses.

    • @PGWalkthrough
      @PGWalkthrough Před rokem

      I have an interesting question, can we use auth0 just for authentication then if user is authenticated when they hit our api we add our own api custom claims to the token to handle authorization?

    • @randomphilic27
      @randomphilic27 Před rokem

      Looking forward to that one actually!
      Please do explain and show roles implementation in detail

    • @StefanOstojic98
      @StefanOstojic98 Před rokem

      Can't wait! :D

  • @matasbernotas5036
    @matasbernotas5036 Před rokem +122

    I think more topics about authentication and authorization in general would be great! Its necessary for nearly every project these days but there is so little good content online or and written by unknown people that you are not sure if you can trust!

  • @axelbreekweg
    @axelbreekweg Před rokem +27

    There are plenty of videos out on this topic, yet your teaching style I find best.
    Please continue this topic also explaining how to refresh the tokens!

  • @nickandrews1985
    @nickandrews1985 Před rokem

    Glad to see you mix it up a bit by going back to some of the basic stuff nearly every application needs. I've used JWT authentication in a few apps, but I learned a few new tricks from this, thanks Nick!

  • @DeophobicMind
    @DeophobicMind Před 9 měsíci

    can't believe I've been your subscriber all this time until I needed this video. By far the most concise explanation I've seen about jwt implementation.

  • @VaragornX
    @VaragornX Před rokem

    Great timing my dude! Want to implement JWT in my app atm.

  • @timcesar1
    @timcesar1 Před rokem

    Thank u for sharing this topic looking forward to more advanced options using this

  • @skylolo1101
    @skylolo1101 Před rokem +20

    Great content as usual Nick! Keep it up and yes, please show us how you would do refresh token (I already implemented it but I find your implementations always cleaner and well thought of). Thanks! PS: maybe you can show how to add multiple Identity providers (facebook + google + custom) all at the same time.

  • @local9
    @local9 Před rokem +1

    Perfectly turned up just when I needed it.

  • @VahidRassouli
    @VahidRassouli Před rokem +1

    Thank you Nick, great video as always!
    It would be great also to have a video about Refresh Tokens too. Thanks

  • @11r3start11
    @11r3start11 Před rokem

    the refreshing part was always tricky for me, so Im interested in dedicated video A LOT :)

  • @spori858
    @spori858 Před rokem

    If it had been made 4 years ago, it would have saved me a lot of headaches. Now it was very refreshing and enjoyable.

  • @NoName-1337
    @NoName-1337 Před 4 měsíci

    Thank you for this video. It was a great help for beginning with this topic. Would like to see some deep-dive videos into this topic.

  • @seba123321
    @seba123321 Před rokem

    Finally full JWT tutorial. Thanks!

  • @Naithe
    @Naithe Před rokem

    very helpful. would very much like to see more about this topics like refresh

  • @julienraillard3567
    @julienraillard3567 Před rokem +1

    Thanks for this wonderfull new video as always ;)
    I really would like you to show an implementation of refreshing JWT stored in any vault you want if it is possible :D

  • @ihorvelychko5254
    @ihorvelychko5254 Před měsícem

    Great video and JWT Auth explanation. Thanks for the work

  • @ghkpr
    @ghkpr Před rokem

    Hey, Thanks for the great video, easy to follow, straight to the point. Would be nice if you could make one about refresh tokens.

  • @indiefold
    @indiefold Před 5 měsíci

    Thanks for this video!
    Was missing the correct issuer url in my case.

  • @mehrankhan5410
    @mehrankhan5410 Před rokem +3

    Hey Nick, thanks for this video and it would be far better if you make a video about the implementation of OAuth

  • @idrisAkbarAdyusman
    @idrisAkbarAdyusman Před rokem +1

    Nice video! i'd love to see the video for the refresh token as well

  • @mikereznikov5521
    @mikereznikov5521 Před 9 měsíci

    GOGO Nick!
    In postman, instead of adding header Authorization, having Bearer (space) token
    U may get the same results easier going to Authorization tab (1 left from Headers), choose Type Bearer Token and just past the token in the right panel

  • @Dustyy01
    @Dustyy01 Před rokem +1

    Very nice video to teach the concepts🎉

  • @dmitri2366
    @dmitri2366 Před rokem

    This came just at the right moment!

  • @yupii1997
    @yupii1997 Před rokem +1

    Hey Nick great video as always . I would love to see your approach for JWT tokens for implementing where a user can perform update and delete operations only for posts that are created by them.

  • @magicspider8
    @magicspider8 Před rokem +4

    I am doing this with okta but I use a combination of middleware and filter to programmatically add the policy to the Controller/Action. I was able to inject javascript to swagger and add the header programmatically to curl so that you don't have to use the swagger authorize UI manually.

  • @vincentbergeron518
    @vincentbergeron518 Před rokem +5

    A refresh token video would be coll. And also how to use JWT with asymmetric keys

  • @irfanshaik1302
    @irfanshaik1302 Před 5 dny

    This is super helpful. Thanks!

  • @georgekalokyris
    @georgekalokyris Před rokem +1

    Great video - thanks Nick. Refresh token video please :)

  • @Lidemann92
    @Lidemann92 Před rokem

    I needed this video 4 days ago 😅 Do one for refreshing tokens aswell

  • @rusektor
    @rusektor Před rokem +4

    What is the code of TokenGenerationRequest class?

  • @user-dj9ys9tq4x
    @user-dj9ys9tq4x Před 3 měsíci

    straight to the point. thanks!

  • @Otonium
    @Otonium Před rokem

    Good video and it world be nice to have a video about refreshing

  • @raphaelyaadar1645
    @raphaelyaadar1645 Před rokem +1

    Great video!. Kindly show us how to refresh token as well. Thanks in advance

  • @francisgauthier4160
    @francisgauthier4160 Před rokem +1

    Hi Nick very nice videp, please make a video about the refresh token. I am currently implementing this on a projet!

  • @mikemcwilliams6758
    @mikemcwilliams6758 Před rokem

    This is an awesome video regarding claims and policies. The challenge though is to accept many claims under one policy; this works well when you have a user that falls under several claims. However, I think that the Authorization Attribute approach may be a better solution. With that said Nick, are those attributes "stackable" on a controller method?

  • @hevymetldude
    @hevymetldude Před rokem +1

    Would be cool if you show in one of the next videos how to authenticate against Azure AD and lock down a Web-Application, so only users can use it when they are member of a specific group. :)

  • @stanislavmasa3707
    @stanislavmasa3707 Před rokem

    Thanks for the video! Will you do some auth videos about Blazor WASM in the future? What about some resource-based authorization?

  • @rustamhajiyev
    @rustamhajiyev Před rokem +5

    If only you could give us OpenIdDict series. There is almost no content on this topic. I believe a lot of people will appreciate it :)

    • @rapzid3536
      @rapzid3536 Před rokem +2

      I was shocked at how sparse the OpenIdDict documentation was.

  • @Hantick
    @Hantick Před rokem

    Guys what would you recommend where i have role per resource (entity in table) which can be created by any user but then only Owner of entity can perform delete, update etc. For now I have just database call validating if user has Owner role in the resource, but wondering if there is better approach

  • @KhauTek
    @KhauTek Před rokem

    can we do refresh tokens too? i would like to know if some implementation i used is a good one

  • @kudorgyozo
    @kudorgyozo Před 8 měsíci +3

    Source code would've been nice!

  • @dsowa
    @dsowa Před rokem +17

    Instead of using an additional policy or attribute, i would suggest to add the custom claim "admin" as a "role" claim:
    var claims = new List()
    {
    new Claim(...),
    new Claim(...),
    new Claim("role", "admin"),
    new Claim("role", "..."),
    Then the controller/actions can be protected by using:
    [Authorize]

    • @jfpinero
      @jfpinero Před rokem

      This should go in a handler that is associated to the policy instead of hardcoding directly in the controller

    • @rankarat
      @rankarat Před rokem

      I always thought this is the best and most basic approach.

    • @AthelstanEngland
      @AthelstanEngland Před 8 měsíci

      I think he did say that but just used admin like this for ease.

  • @zagoskintoto
    @zagoskintoto Před rokem +7

    Great video. What about api keys? Is there a clean way to have an endpoint require jwt auth, another having api-key auth requirement, and have both for the rest? An example of this would be great! I've done same like these in the past but never found a clean way to do it. I've tried looking into making an auth handler to implement permissions and having the api key be a permission but can't seem to get it the right way

    • @VapidLinus
      @VapidLinus Před 6 měsíci

      Did you find a solution for this use case?

  • @alexandershubert573
    @alexandershubert573 Před rokem

    Thank you indeed!

  • @DaminGamerMC
    @DaminGamerMC Před rokem

    Hey Nick, do you recommend using blazor for frontend?

  • @lennarthammarstrom1321
    @lennarthammarstrom1321 Před rokem +1

    An advanced feature that I've seen a million ways that would be awesome to see you discuss would be how we can do "Enterprise Isolation". What I mean with this is that say that we have a SaaS where an "Enterprise" can have a subscription and manage their own details. For the sake of simplicity the developers want to have one shared database for all customers, which makes setup easy but it's equally dangerous because were just one missing .Where(x => x.EnterpriseId == _userEnterpriseId) from exposing other customers data. How would you setup that sort of thing? For example query filters in EF are awesome, but they don't help when patching / adding items.

  • @josephh8117
    @josephh8117 Před rokem

    I already have oauth as my default authentication scheme for this one dotnet core app, can I tack jwt on to that?

  • @UmutDereWork
    @UmutDereWork Před 3 dny

    I like to see a new video about authorization & authentication topics on .net 8

  • @buusouza
    @buusouza Před rokem

    Hey, how would we do that in a azure function? i'm struggling with that

  • @jeroenvanlangen8953
    @jeroenvanlangen8953 Před 8 měsíci

    I didn't get the names of where to store the keys instead of the configuration? (did you say aws secrets manager?)
    What would you use to store those keys for a local project?

  • @ahmedma527
    @ahmedma527 Před rokem

    Thanks for the great video. I have one question. Why can't a hacker get the token from the network tab like any developer, but in the production environment? I know it is not possible and it would have been a disaster but why? Thanks again.

  • @birukayalew3862
    @birukayalew3862 Před rokem

    NIck a great video!! can you show the authorization using permission based please?

  • @levmatta
    @levmatta Před rokem

    How to manage the token in JavaScript? How to make external complements use the token and be resistant to refresh. Thanks

  • @alisonhj
    @alisonhj Před 9 měsíci

    Hello! Thanks for sharing this video! Would you also be willing to share the Github repo for this examples as well?

  • @user-rd4oo1jg5g
    @user-rd4oo1jg5g Před 11 měsíci

    What is the difference between Claim and Signature? what does each one reference?

  • @HeyWhoTheFuckAreYou
    @HeyWhoTheFuckAreYou Před rokem +3

    How about making a video about OpenIddict about OAuth and OpenIDConnect?

  • @carlosbaptiste2062
    @carlosbaptiste2062 Před rokem

    @Nick could you do a video on a hybrid approach? Oidc like Okta/Auth0 for authentication (AUTHN) and local claims for Authorization (AUTHZ)

  • @JohnnyCoRuyzo
    @JohnnyCoRuyzo Před rokem

    It's posible to have both identity validation for login UI and JWT authorization?
    I was trying this last weekend, and couldn't make them work at the same time.
    Love your content 🤙🏻

  • @mayureshs80
    @mayureshs80 Před rokem +1

    Nice basic video. Would love to see refresh token video.

  • @cheynelothering7461
    @cheynelothering7461 Před 5 měsíci

    As what did you declare the CustomClaim in your TokenGenerationRequest ?

  • @VladyslavHorbachov
    @VladyslavHorbachov Před rokem +1

    Can you make a video about refresh tokens? It would be great ❤

  • @rrkatamakata7874
    @rrkatamakata7874 Před rokem

    What about asymmetric encryption. What is the best practice for api projects.

  • @cburys
    @cburys Před 9 měsíci

    hands down the best explanation online. ty sir!

  • @kippie86
    @kippie86 Před rokem +2

    Hey Nick,
    Is there a specific reason why you use a custom claim and policy for your admin users instead of simple role based authorization? Or was this just done for the sake of showing off the custom policies?

    • @nickchapsas
      @nickchapsas  Před rokem +6

      It’s for the sake of showing off custom policies. I’m trying to have a basic barebones video so I can later do an advanced one that will show those

  • @0x4b55
    @0x4b55 Před rokem

    No word on [Authorize(Roles = "Role1,Role2")] or User.IsInRole? ... which maps to the (default) role claim in the JWT

  • @Cornet435
    @Cornet435 Před rokem +1

    you could mention about Roles, for example Authorize(Roles = „admin”)

    • @fonskeee
      @fonskeee Před rokem +1

      Thats the old way, now you better add the role as a claim to your policy, that way its mutch better to manage when your application grows or if some authorization stuff changes you just need to do it in one place

  • @emilyanapenarandaanagua3529
    @emilyanapenarandaanagua3529 Před 3 měsíci

    Hello! Love the video, I am introducing myself with it, I have some doubts about the project of Identy.API
    Someone has the code source or can explain how implement it?

  • @user-bb1mf6uo6n
    @user-bb1mf6uo6n Před rokem +3

    Would be good to see how auth and Blazor WebAssemble play together :-)

    • @JohnnyCoRuyzo
      @JohnnyCoRuyzo Před rokem

      I have the same interest, couldn't make it work

  • @iamaashishpatel
    @iamaashishpatel Před 11 měsíci +2

    Is the sample source code available on GitHub?

  • @pemifo260
    @pemifo260 Před měsícem

    We want to see a video about refreshing tokens!

  • @acmesoft
    @acmesoft Před 11 měsíci

    Can you make a video with Blazor WASM with Cognito Authentication thats uses groups to profiling the app content? Thank you!

  • @florianvanbondoc3539
    @florianvanbondoc3539 Před 9 měsíci

    Nice channel ❤

  • @mrt7948
    @mrt7948 Před 5 měsíci +12

    this is very confusing

  • @loganyoung228
    @loganyoung228 Před měsícem

    So authentication/authorization works on the api via an Identity provider. Fine, I'm okay with that. What I don't understand is how is my client application, say a razor pages app, supposed to work with this? I send a username and password to my API to login, the API returns the token and we're all great. What do I do with it then? I can embed it in an HttpOnly secure cookie, but that isn't enough to authorize the user to perform actions on the razor pages app, right? So how do I configure my app to use the token from the API to infer authorization status of a user?

  • @maherhujairi1601
    @maherhujairi1601 Před rokem

    i would have done by having the initial service implement the handler too and this way to can add this new pattern into the existing code until you need a real reason why it has to be it's own set of handler classes .. this way you can introduce this pattern into an existing code without having to through the code you already had.

  • @PelFox
    @PelFox Před rokem

    When do you really create and manage this yourself compared to using oidc providers like IDS or Auth0?

  • @grzegorzr3621
    @grzegorzr3621 Před rokem

    Great content. How to solve the case when the endpoint is accessed by the admin or the owner of the resource (move), e.g. downloaded from the DB?

    • @jfpinero
      @jfpinero Před rokem

      Associate a handler to the policy that checks the role in the claim with your determined list of allowed roles for the resource. You can also look up resource based auth.

    • @Hantick
      @Hantick Před rokem

      ​@@jfpinero what if we also have roles tied to a resource. In example for application user has "User" role but in the resource it is Owner or Participant, where only Owner can perform Update, Delete actions ? Claims seem to be too much complicated to keep relationship between user roles and resources. For now i just make a call to a database to check if user X has role Owner in entity A

  • @gabrieldai88
    @gabrieldai88 Před rokem

    I have a problem in my API when not sending the token, instead of 401 i'm getting 500. does anyone knows why that happens?

  • @jeroenvanlangen8953
    @jeroenvanlangen8953 Před 8 měsíci

    It looks like a lot boiler plate code. There are not standard components for this?

  • @georgekon2007
    @georgekon2007 Před 11 měsíci

    Did you ever use OpenIddict ?

  • @ClickOkYT
    @ClickOkYT Před rokem +1

    I want to see the consumer part. I mean, let's say I create a blazor/razor/react/angular/etc frontend, and now I want to use this api, ie, login, access protected endpoints, etc.

  • @AddictOfLearning
    @AddictOfLearning Před rokem +2

    Please add video to security refresh tokens

  • @kartikkolte6025
    @kartikkolte6025 Před 8 měsíci

    @nickchapsas first of all thank you for sharing such good stuff always ... I am dying to see your video for refresh tokens have you made it if yes please give me link

  • @JamesSecretofski
    @JamesSecretofski Před rokem +1

    you can actually set auth bearer token in authorization tab in postman. just saying

  • @GlebWritesCode
    @GlebWritesCode Před rokem

    One of the previous projects' lead insisted on storing user data inside the token as separate claims. Stuff like email, phone, country of residence. The reason was - not to query the DB (speed optimization). Is that a good idea? What if your phone changes and youre using the token with old data?

    • @FurqanaFathuzzaman
      @FurqanaFathuzzaman Před 10 měsíci

      This might no longer relevant to your old question, but how often do you need phone number. It might make sense if EVERY endpoint you have require phone to be validated or have a use of it. Otherwise claims should carries only the minimum info needed.
      As for phone changes (in the case that you REALLY need it in the token), how often does it change? Remember that JWT is usually short-lived, so it should refresh on the next one.

    • @GlebWritesCode
      @GlebWritesCode Před 10 měsíci

      @@FurqanaFathuzzaman we needed phone number and email for quite some calls. As for a token - I don't know if it's best practice, but for this system it was not 5 minutes

    • @FurqanaFathuzzaman
      @FurqanaFathuzzaman Před 10 měsíci

      @@GlebWritesCode There are also other techniques such as caching if you simply don't want to hit the database often.

    • @GlebWritesCode
      @GlebWritesCode Před 10 měsíci

      @@FurqanaFathuzzaman My thoughts exactly. Its much easier to update too

  • @boommonkey111
    @boommonkey111 Před rokem +1

    we used IdentityServer now moving over to OpenIdicct... massive pain in the ...

  • @nelsonrivers8546
    @nelsonrivers8546 Před rokem

    Can you show how to "Refresh JWT Token" ?

  • @abdelmoumenbenaida8037
    @abdelmoumenbenaida8037 Před 8 měsíci

    @nickchapsas Can we have source code used for this video ?

  • @hanaasihanish
    @hanaasihanish Před rokem +1

    Hey, Can you make a video on Identity API

  • @ShaZeGaming
    @ShaZeGaming Před 9 měsíci

    Can anyone tell me where s the config came out at 4.50 ??

    • @MrTer1yaki
      @MrTer1yaki Před 5 měsíci

      Loads the appsettings config file with var config = builder.Configuration; in startup.

  • @Krummelz
    @Krummelz Před 6 měsíci

    Why would you HAVE to move to an identity provider if you could just issue your own tokens?

  • @hakanfostok2547
    @hakanfostok2547 Před 10 měsíci

    Hi Nick, you always say the link to the code in the description below, but sorry, I have never seen a link to the code in the description in any video, Am I miss something?

  • @samiechan2298
    @samiechan2298 Před rokem

    What about Refresh tokens?

  • @TimmyBraun
    @TimmyBraun Před rokem

    Minimal API with FastEndpoints FTW!!

  • @atreyyu1
    @atreyyu1 Před rokem +1

    Why do you think it's acceptable to omit random words in speech?

  • @gp6763
    @gp6763 Před rokem

    Wait, we can use the James Webb Telescope to authenticate ourselves? 😅

  • @cmugy
    @cmugy Před rokem

    I am currently implementing something similar at work😅

  • @radekfilonik639
    @radekfilonik639 Před rokem

    We need RefreshToken video ❤

  • @onlycode8100
    @onlycode8100 Před rokem

    Now waiting for Refreshing Token because without it this is only half implementation :)