How to Implement Refresh Token in ASP.NET Core Web API

Поділитися
Вставка
  • Опубліковано 12 гру 2024
  • ►► Check out our courses: bit.ly/cdmz-co...
    ►► Support us on Patreon and get the source code: / codemaze
    In this video, we are going to learn about refresh tokens, their use in modern web application development, and how to implement this feature in the .NET Web API using the latest framework.
    Refresh tokens are credentials that can be used to acquire new access tokens. When an access token expires, we can use a refresh token to get a new access token from the authentication component. The lifetime of a refresh token is usually set much longer compared to the lifetime of an access token.
    LINKS MENTIONED IN THE VIDEO
    ►► Global Exception Handling: • Global Exception Handl...
    FOLLOW US ON SOCIAL MEDIA!
    ►► / marinko-spasojevic
    ►► / codemazeblog
    ►► / codemazeblog

КОМЕНТАРІ • 47

  • @CodeMaze
    @CodeMaze  8 місяців тому

    Thank you all for watching and for your support.
    ►► If you want to check out all our courses you can do that here: courses.code-maze.com/courses/

  • @ElohimCode
    @ElohimCode 10 місяців тому +1

    You made refreshtoken implementation simpler. Thanks for sharing 😊

    • @CodeMaze
      @CodeMaze  10 місяців тому

      Glad it was helpful! Thank you for watching.

    • @PowersoftIT
      @PowersoftIT 3 місяці тому

      How to implement from client application like angular or MAUI

  • @I-PixALbI4-I
    @I-PixALbI4-I 10 місяців тому +1

    Just yesterday implement refresh token but a bit different, using cookies for refresh token.
    Thx for video!

    • @CodeMaze
      @CodeMaze  10 місяців тому

      Thank you for watching. Yeah, it can be done that way as well. For Web APIs I usually lean a bit more to JWT comparing to server-side apps like Blazor Server or Razor Pages, where I would probably go with cookies as well.

  • @Sharondevbd
    @Sharondevbd 4 місяці тому

    Great! I Hope I Founded the right Tutorial & Channel

    • @CodeMaze
      @CodeMaze  4 місяці тому +1

      I hope so too! Thank you for watching the video.

  • @zameer.vighio
    @zameer.vighio 5 місяців тому

    Thanks first.
    Excellent way of explaining

    • @CodeMaze
      @CodeMaze  5 місяців тому

      You are most welcome.

  • @johannes3980
    @johannes3980 7 місяців тому

    I have a question. When I have a MAUI app as the client, for example, what is the best practice for the refresh flow to maintain a high user experience? Because when the access token is invalid, it would take six calls until I have the data if the token needs to be refreshed. So, should the token be refreshed in the background if it’s expired to maintain a high UX?

    • @CodeMaze
      @CodeMaze  7 місяців тому

      Hi. I never worked with MAUI so I am not sure what is going on there, but usually, on the client app, you should have some sort of interceptor that will intercept the request and check the token you are sending with the request and if it is about to expire or expired, you can first send the request to the Token endpoint to refresh the token, and then attach that new token to the request. That's the case with JWT auth.

  • @tanoryjakaperdana1419
    @tanoryjakaperdana1419 7 місяців тому +1

    Why if token.expire

    • @tanoryjakaperdana1419
      @tanoryjakaperdana1419 7 місяців тому

      Ah sorry my bad.. im a bit sleepy and didnt read well 😅

    • @CodeMaze
      @CodeMaze  7 місяців тому +1

      So, you understand why?

    • @tanoryjakaperdana1419
      @tanoryjakaperdana1419 7 місяців тому

      ​@@CodeMazei missread it.. its if refreshtokenexpire

    • @CodeMaze
      @CodeMaze  7 місяців тому +1

      Ah ok. Makes sense. I thought you were talking about refresh token from begining :)

    • @tanoryjakaperdana1419
      @tanoryjakaperdana1419 7 місяців тому

      @@CodeMaze haha sorry.. im trying to use sigin with google button (not google auth) and then convert the credential from google to jwtauth.. thanks for the video

  • @alexalexander3252
    @alexalexander3252 3 місяці тому

    Hello, great video. Thou I can not get the following. What is "principal.Identity.Name" construction in RefreshToken method? Where does this "identity" property comes from? As you did not populate this prop when creating GetClaimsPrincipalFromToken method. I have a NULL in this value.

    • @CodeMaze
      @CodeMaze  3 місяці тому

      Hi. It is the Name claim that you assign while creating the token. Identity represents a property from the ClaimsPrincipal class and that property gets the primary claims of the current principal.

  • @coder3123
    @coder3123 10 місяців тому

    if someone gets hold of refresh token would they not able to use it to generate as many access tokens as they want?

    • @CodeMaze
      @CodeMaze  10 місяців тому

      Well, yes. But that's really dangerous case. Basically, look at it this way, if someone gets keys of your appartment, what you can do until you notice that. This is something we try to avoid by storing the refresh tokens safely in our client applications that uses the Web API. On the client you can encrypt it and store it so if by any chance someone gets it, it would be meaningless to them. But if someone can intercept your HTTP requests, than you have bigger issues. Also, refresh tokens are not only about security but also about better user experience so we need to look them that way as well.

  • @salihe1232
    @salihe1232 3 місяці тому

    why are we encyrpting refresh token with RandomNumberGenerator Class can't we make that JWT too

    • @CodeMaze
      @CodeMaze  3 місяці тому

      Hi. No, I am not encrypting a refresh token. I am using that mentioned class to generate one. Refresh token isn't the same as the access token, so you don't need another JWT. You don't need claims and other stuff JWT has inside.

    • @salihe1232
      @salihe1232 3 місяці тому

      @@CodeMaze So i want to publish a api and every api developer is doing like this way.So you say it is secure to publish it like that we don't need to encrypt the refresh token

    • @CodeMaze
      @CodeMaze  3 місяці тому

      You can do it this way. If you have a client app that consumes your API and stores the tokens in local or session storage, you can encrypt the access token inside the client app first, and then store it in the storage. Of course, everytime you send that token with a request, you need to decript it first.

    • @salihe1232
      @salihe1232 3 місяці тому

      @@CodeMaze Thank you !!😇

  • @LuckyBwaromalePaul
    @LuckyBwaromalePaul 6 місяців тому

    Thanks for the video. Appreciate

    • @CodeMaze
      @CodeMaze  6 місяців тому

      My pleasure! Thank you for watching.

  • @mohamedroshdy4916
    @mohamedroshdy4916 10 місяців тому +1

    can share with us this demo😇

    • @CodeMaze
      @CodeMaze  10 місяців тому

      Hi. We will soon provide the option for the source code.

  • @ahmedrizk106
    @ahmedrizk106 4 місяці тому

    this approach does not support user logging from multiple clients since there is only one refresh token/user at a time then once the first client consume it the second client will log out the user.

    • @CodeMaze
      @CodeMaze  4 місяці тому

      You are correct, and you shouldn't support that as well as it can be resky and so many companies abandoned that approach. Now, if you have a single user, you should be logged in from a single client.

    • @ahmedrizk106
      @ahmedrizk106 4 місяці тому

      @@CodeMaze how can you abandon supporting multiple clients when most apps have a mobile app and a web app and the user can be logged in on both at the same time ?

    • @CodeMaze
      @CodeMaze  4 місяці тому

      No, those are not the same. Web app and the mobile app are not using the same method. If you are using a mobile phone to access the web app, than it is the same thing and it shouldn't be supported. But mobile apps are different things.

    • @ahmedrizk106
      @ahmedrizk106 4 місяці тому

      @@CodeMaze sorry I misspoke what I meant was a mobile app and a web app accessing the same apis or maybe you are logged in from your laptop and your pc at the same time which is a common scenario how can you solve this ?

    • @CodeMaze
      @CodeMaze  4 місяці тому

      I am not solving that. As I said, when you have an API and a usual SPA client that consumes that API, it is a security risk to allow a single user to be logged in from multiple browsers. Because of that, for me, it is the rule that the user can use only a single browser/tab and be logged in.

  • @weradsaoud2018
    @weradsaoud2018 6 місяців тому

    Thank you

    • @CodeMaze
      @CodeMaze  6 місяців тому

      You're welcome. Thank you for watching.

  • @Famouse
    @Famouse 10 місяців тому

    Can you give a link to the source code?

    • @CodeMaze
      @CodeMaze  10 місяців тому

      Hi. We are working on it. Soon, you will be able to get the source code for all the videos.

    • @Famouse
      @Famouse 10 місяців тому

      Well, thank you.@@CodeMaze

  • @Famouse
    @Famouse 9 місяців тому

    Is there a link to the source code?

    • @CodeMaze
      @CodeMaze  9 місяців тому

      I am really sorry, but it will be soon an option for that as well. It is a bit harder as this is part of our book's source code and I have to divide it somehow to leave only the relevant parts there. It would be unfair for the people who purchased the book to share the source code of the entire project.

    • @Famouse
      @Famouse 9 місяців тому

      Ok. No problem.@@CodeMaze

  • @shahidwani6445
    @shahidwani6445 6 днів тому

    What if hacker gets both tokens

    • @CodeMaze
      @CodeMaze  5 днів тому

      There is nothing much to say there - you have a serious security issue then. This simply shouldn't happen. But if you have a client app, and you store your tokens in some sort of storage, you can encrypt them in the client app first and then store them. That way, if someone steals them, the tokens will be of no value to them. Of course, you have to remember to decrypt them before using them in the request.