Implementing JWT Authentication in ASP.NET Core

Поделиться
HTML-код
  • Опубликовано: 26 дек 2024

Комментарии • 90

  • @mynameisshadywhat
    @mynameisshadywhat 2 месяца назад +31

    The first time in a long time I needed a refresher on this and this is the first recommended video, posted only 17 minutes ago. What sorcery is this, Nick?

    • @nickchapsas
      @nickchapsas  2 месяца назад +43

      I’m behind you

    • @willbo_
      @willbo_ 2 месяца назад

      (⊙ˍ⊙)

    • @aracon9721
      @aracon9721 2 месяца назад +2

      it's called youtube algorythem *smirk*

    • @SunriseTequila523
      @SunriseTequila523 2 месяца назад +1

      ​@@nickchapsasDo you have a course on sql for ef core developers?

    • @vornamenachname906
      @vornamenachname906 Месяц назад +1

      That's called a kind of P-hacking.
      You internally calculate "wow how low was the chance, that this specific channel releases a video about this topic now"
      But in reality you have many channels and probably many queries/demands. The chance that one of them by one of them is fullfilled is much higher.

  • @AJIexa8
    @AJIexa8 2 месяца назад +22

    Great video! I would suggest to people who are new to JWT first learn fundamentals about OpenID Connect and authorization methods like "Authorization Code Flow" or " Resource Owner Password Grant Flow". Once you understand the flow you will get that Cognito, IdentityServer4 or any other providers are just wrappers around this protocol

    • @DustinKingen
      @DustinKingen 2 месяца назад

      I wouldn't recommend IdentityServer4 since it's no longer maintained. Instead use Duende IdentityServer (commercial) or OpenIddict.

    • @hero3616
      @hero3616 2 месяца назад +1

      ROPC flow is obsolete and it should not be used unless your api and STS are on the same server

  • @conniedecinko9245
    @conniedecinko9245 2 месяца назад +4

    We're using Keycloak for both Java and .NET apps

  • @EduardLlull
    @EduardLlull 2 месяца назад +2

    I think you should comment that once you start using AWS Incognito service, you don't need the "token generator API" anymore. In my experience, most developers brains shortcircuit when they try to understand oidc and some may think they still need the API even after they start using Incognito because they don't understand how it works.

  • @tomtoups
    @tomtoups 2 месяца назад +13

    Changing the first letter "e" is not a good test for if the JWT is working, because the "e" is the base64 character for the open brace "{" for the JSON. That's why all JWTs start with an "e". It could be failing because it's not a valid JSON fragment

    • @SlyEcho
      @SlyEcho 2 месяца назад

      They all start with "ey". It's like the Matrix, you start to see the code and recognize it if you look long enough.

    • @hero3616
      @hero3616 2 месяца назад

      I don’t think author knows details

    • @codecomposer88
      @codecomposer88 Месяц назад

      You can always decode a base64 string. Nothing is secret about that. The point of changing the content of the token payload was to show that as soon as the payload is changed, the token is invalid because the content no longer matches the signature verification.

  • @Octopie18
    @Octopie18 2 месяца назад +11

    The modern way to create JWTs is using the new `JsonWebTokenHandler` class, not the older & slower `JwtSecurityTokenHandler` class.

    • @nickchapsas
      @nickchapsas  2 месяца назад +4

      You are right. I used my old codebase for this video instead of the new one that used the WebToken one 🤦

  • @frossen123
    @frossen123 2 месяца назад +3

    2:30 "It's a piece of string!"😄love it

  • @DasBloch
    @DasBloch 2 месяца назад +9

    It would be awesome if you could show how to support multiple authentication methods. Like an API key together with jwt tokens. How do we correctly authorize endpoints and setup the auth ?

  • @PhantomPhobos
    @PhantomPhobos 2 месяца назад +9

    Wish to see more on this topic, I started off by rolling own ID server using OpenIddict, was quite a rough start first time learning in the whole OpenID process 😅. Video on rolling your own ID server would be nice, even better if it's using OpenIddict. Still waiting for more videos on event sourcing too, only see one in your channel, perhaps those are locked behind patreon?

  • @andersborum9267
    @andersborum9267 2 месяца назад +5

    It's good starting video on using an external Idp, but don't forget that what's shown here is the bare minimum. Also, notice that Nick is using a stand-alone UI (i.e. Postman or similar) to exchange the returned auth code for a set of tokens that's stored locally (Redis is a great option for these types of short lived data); you'll need to build the exchange mechanism yourself, which is a fun little task.
    A word of caution: security is not something you should take lightly, especially in an public facing environment and you should never attemt to implement encryption algorithms, unless you're one of the very few experts in the industry, that works professionally on this topic.

    • @aracon9721
      @aracon9721 2 месяца назад

      so pro-apprentices like me probably still better use Auth0?

    • @insteor
      @insteor 2 месяца назад

      @@aracon9721 not only you, but almost everyone, except if you work in that Cognito team. as I've heard - never write your own logging and auth.

  • @Paps526
    @Paps526 2 месяца назад +1

    Funny thing is that I made exact same implementation with keycloak yesterday for the first time

  • @vintage8
    @vintage8 2 месяца назад +4

    Thanks, couple questions tho:
    How should I sync the users to my db with external id providers?
    Should I store roles, permission in my db or in the external provider?
    and also shouldnt client secret be secret? I shouldnt use it in a spa, right?

  • @MarvinKleinMusic
    @MarvinKleinMusic 2 месяца назад +5

    It would be pretty intersting how you can secure a Blazor app with JWT.

    • @codecomposer88
      @codecomposer88 Месяц назад

      Same way as the API example in the video. The authorization feature for a blazor component is decoupled from the authorization method. For example, for a product I made a blazor app authorizing users with OpenID through Azure AD but the components themselves has no idea HOW the user is authorized. They just allow access IF the user is autenticated and - in some case - is authorized with certain roles. In this case the OpenId could easily be replaced with a simple login using JWT without changing anything in the blazor components.

  • @justinassakalavicius4465
    @justinassakalavicius4465 2 месяца назад

    Great video! I would like to hear more about the other option you mentioned in your video. Server-side applications with confidential clients

  • @paulguk
    @paulguk 2 месяца назад +3

    I much prefer jwt.ms over Auth0's decoder. Much simpler page and also decodes the iat and exp values etc.

  • @local9
    @local9 2 месяца назад

    This couldn't of come at a better time.

  • @festussila8859
    @festussila8859 2 месяца назад +1

    @nickchapsas Great refresher. I find it difficult to establish a resources that outlines at length the ins and outs of security(providers, users, flows, scopes, policies, federation). As part of the courses offered on Dometrain do you have a plan to do a comprehensive course on the subject.

  • @christianschieder3140
    @christianschieder3140 2 месяца назад

    I'd like to see something simular also showcasing the approach via azure (if possible not sure). And maybe also with something more restrictive configuration(if possible of course), like enabling google or facebook login, where you can customize which google emails can register but , restricted to a specific domain. Or for the facebook side enabling registration for specific users(ofcourse if possible).
    Otherwise again a nice lecture, i enjoy your videos, keep it up👍

    • @hero3616
      @hero3616 2 месяца назад

      It’s called B2C in Azure and free up to 50K users

  • @kidsam27
    @kidsam27 2 месяца назад +1

    Have you ever considered or compared this to something opensource and self hosted like key cloak? I've used it in a couple of project and i has pretty much all you've showcased but it's bit more quirky.

    • @PelFox
      @PelFox 2 месяца назад

      Keycloak follows the oidc/oauth2 protocol and comes with lots of identity providers, user management and roles. It's a complete solution compared to this example which just creates a JWT token from hardcoded values.

  • @vasilyh4588
    @vasilyh4588 2 месяца назад +2

    What about roles check during API call?

  • @margosdesarian
    @margosdesarian 2 месяца назад +2

    Hey Nick, can you make a video about which is the nicest butter?

    • @petewarner1077
      @petewarner1077 2 месяца назад +1

      Let's demand a #buttercop series in which Nick takes a look at terrible butter advice posted on LinkedIn and signs off with "But now I want to know about you.... which butter do YOU think is the nicest butter? Are you using butter in your applications? Or a healthy low-fat substitute?"

  • @LilPozzer
    @LilPozzer Месяц назад

    These Boots Have Seen Everything

  • @checox1909
    @checox1909 2 месяца назад

    Nick which SO do you use?

  • @SuperAndrea000000
    @SuperAndrea000000 Месяц назад +1

    It would be awesome if you could make a video about how to implement a refresh token mechanism in my API project.
    My API issues a JWT with a refresh token, but when the browser makes multiple requests in parallel, sometimes they fail because the first one refreshes the JWT, while the others fail because the refresh token has already been used. It would be nice to see how you'd solve this problem.

    • @hck1bloodday
      @hck1bloodday Месяц назад

      may be implement a retry mechanism on the frontend?

  • @ibnfpv
    @ibnfpv 2 месяца назад

    What about the user in DB?
    Manage additional data ?
    That you don’t want to exposed in jwt
    How you will merge the auth flow with real application need like current user in a request context
    Gather additional info from db and so on
    In best practice from your perspective

  • @hristoivanov6436
    @hristoivanov6436 Месяц назад

    Nice video! Could you show us S2S auth process?

  • @alanis4AL
    @alanis4AL 2 месяца назад

    When i see you doing it seems graspable
    Why everyone else claims that authentication and authorization are the holy grail of programming? Concepts so difficult to grasp that you need a doctorate degree!?

    • @cocoscacao6102
      @cocoscacao6102 2 месяца назад

      It's poorly documented in certain areas, and I've found most of the answers digging through stack overflow. Concepts aren't that difficult, but implementation in .NET can be a she-dog...

    • @diadetediotedio6918
      @diadetediotedio6918 2 месяца назад

      I never saw anybody saying it is "the holy grail of programming", but it is a difficult problem, yeah.

    • @maskettaman1488
      @maskettaman1488 2 месяца назад

      Because the hard work is being done for us by people that know a lot more about it than we do. It's effectively lego-by-design specifically because it's so hard to get right.

  • @TheCodeCreator
    @TheCodeCreator 2 месяца назад

    Can JWT also be used in simple ASP NET Core webapps (no API's), right? Thanks.

  • @yaroslavsolodyankin928
    @yaroslavsolodyankin928 2 месяца назад

    Does .dometrain have any support? What is the problem with the video player?

  • @ezekielgallardo8385
    @ezekielgallardo8385 Месяц назад

    Can you show an example of this using Active Directory?

  • @Arshaad786000
    @Arshaad786000 2 месяца назад

    cool. could you show us how this would work with the microsoft identity tables in SQL

  • @rennasccenth
    @rennasccenth 2 месяца назад +2

    Ok, this bg3 reference got u one more like. Are you happy now???

    • @nickchapsas
      @nickchapsas  2 месяца назад +1

      What an absolute banger of a game

  • @phillipkatete634
    @phillipkatete634 2 месяца назад

    Is there a way to add users to the cognito service (rather than users having to enrol)?

    • @mibli2935
      @mibli2935 2 месяца назад

      Yes. (If I understood your question correctly). Login to your AWS, find Cognito Service, create your UserPool and in the tab "Users" you can do just that.

  • @the_arch
    @the_arch 2 месяца назад

    Any chance of making a video for integration with MS Entra SSO?

  • @chrisstephens2694
    @chrisstephens2694 Месяц назад

    A really clear and concise explanation, as usual! It would be good to see something similar but utilising Entra Id rather than AWS Cognito.

  • @SuperLabeled
    @SuperLabeled 2 месяца назад

    "Trust me" Famous last words :) But in all seriousness, could you use something like this to authenticate your application? Basically making it not possible for anyone outside of your hosted domain to request data from the API?

    • @DisturbedNeo
      @DisturbedNeo 2 месяца назад

      CORS is better suited for that. You can set it up a policy on your server to say “If a request comes from anywhere other than a specific URL, reject it”, and then give it the URL of your client application so that only requests from your client application are let through.

    • @lexmadur
      @lexmadur 2 месяца назад

      @@DisturbedNeo From what I understand about CORS is that it cannot be used to authenticate your application. CORS being only relevant within a browsers JavaScript, they read the CORS headers from the response and raise the error if the URL doesn't match the one in your address bar. Anyone can still make a request and get a response with curl or any other API tool.

    • @hero3616
      @hero3616 2 месяца назад

      @@DisturbedNeocors has nothing to do with server side

  • @I-PixALbI4-I
    @I-PixALbI4-I 2 месяца назад

    What do you mean "Production Ready"? I already implemented JWT to PROD using knowledge from your previous video about JWT!
    o_0

    • @nickchapsas
      @nickchapsas  2 месяца назад +2

      Maybe production ready isn't a good term for this. I wanted to say that we're integrating an independent identity service which is what most modern production apps are using. Maybe "Modern" is better

  • @mhDuke
    @mhDuke 2 месяца назад +1

    definitely make a video for service/machine/server authentication. duh! uh, i mean please :)

  • @SILASGAH
    @SILASGAH 2 месяца назад +1

    Not sure if I am the only one or not, kindly take your time when talking, you seems to be extremely fast when talking. You are educating us

  • @vornamenachname906
    @vornamenachname906 Месяц назад

    Im really wonder why you repeatingly confuse classes and functions , e.g. 3:16 and also in other videos

  • @sachinmaharjan6398
    @sachinmaharjan6398 2 месяца назад

    Hi, NIck, coud you make video about ocelot api gateway with Scalar.AspNetCore

    • @VanDameDev
      @VanDameDev 2 месяца назад +1

      I'd suggest using YARP instead..

    • @sachinmaharjan6398
      @sachinmaharjan6398 2 месяца назад

      @@VanDameDev does YARP can be integrated with scalar for centralize API docs?

    • @VanDameDev
      @VanDameDev 2 месяца назад

      @@sachinmaharjan6398 If you do include scalar in the ApiGateway(YARP) project, I don't see why not..

  • @lucianbumb6690
    @lucianbumb6690 2 месяца назад +6

    I don’t know what other people feel about your way of speaking but I feel tired after listening you for 5 minutes. In my opinion quality doesn’t mean 100 words per minute. In the past your explanations were more human than now. Hope you understand my feedback.

  • @izobrr
    @izobrr 2 месяца назад

    Who else felt odd when Nick wrote just 60?

    • @lylobean
      @lylobean 2 месяца назад

      Don't think when making an advert video he could do 69.

  • @hero3616
    @hero3616 2 месяца назад

    Azure AD (Entra) B2C is way better than Cognito

  • @CharlesBurnsPrime
    @CharlesBurnsPrime 2 месяца назад

    ".NET does not have native JWT integration, so you have to install a Nuget package..." is said like it is a negative thing, but it is a positive one. Why would the core library have support for a specific auth mechanism, of the hundreds that have come and gone?

    • @nickchapsas
      @nickchapsas  2 месяца назад +2

      It’s not said as a negative thing. It’s said as an observation

    • @Otto-the-Autopilot
      @Otto-the-Autopilot 2 месяца назад

      Also the package is still from Microsoft themselves, thus you don't have to rely on some 3rd party library.

  • @molanlabe6543
    @molanlabe6543 2 месяца назад +2

    My gosh, you use so many words to convey so little information.

  • @JacobGlanz1234
    @JacobGlanz1234 2 месяца назад +3

    You’re not explaining what anything means, you’re effectively showing how to copy paste your code

    • @nickchapsas
      @nickchapsas  2 месяца назад +1

      There are 500000 videos explaining JWT including mine. Watch those. I just wanna show the implementation

    • @maskettaman1488
      @maskettaman1488 2 месяца назад

      You're missing the point of the video if you're expecting a dive in to how JWT works

    • @Paps526
      @Paps526 2 месяца назад

      @@nickchapsas I would explain what's MetadataAddress, this has a key role I think in this approach

  • @rohitm8814
    @rohitm8814 Месяц назад

    too fast man!!!

  • @richardrawson381
    @richardrawson381 2 месяца назад +3

    var key="sdfafas"u8; just to learn this notation was worth watching the video 🙂