ASP.NET Core Authentication with Custom Handler

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

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

  • @afzalbazeer600
    @afzalbazeer600 2 года назад +5

    2 days if searching for a well explained video and finally found this. Keep up with the awesome work.
    Could have done better : created a seperate project for custom token without using the same jwt implementation since it might confuse newbies to coding.
    Great work. Thanks again 😊

  • @DevLife717
    @DevLife717 3 года назад

    After searching all day on how to do custom authentication this video finally came thru for me, thanks dude - awesome job!

  • @renekolm
    @renekolm 2 года назад

    Super well explained! Thank you sir!

  • @jaywang7285
    @jaywang7285 4 года назад +1

    Good explain for C# API Authentication ~

  • @hearyourmood9633
    @hearyourmood9633 3 года назад

    if you try to pass options configuration, for example have some property in your options class which inherits from AuthenticationSchemeOptions, it will not configure that object. why is that?

  • @steveareeno65
    @steveareeno65 4 года назад +1

    I am a little confused. I watched the first video on JWT. Why the need for a custom authentication handler?

    • @DotNetCoreCentral
      @DotNetCoreCentral  4 года назад +2

      @Steve W custom authentication handler is an option if a organization already has existing auth token mechanism, in that case a custom handler will help. If you are building a new service then of course you should be using JWT. Hopefully this answers your question. Thanks

  • @Saansh6462
    @Saansh6462 2 года назад

    Great

  • @Suncircle2011
    @Suncircle2011 4 года назад +2

    You don't explain, what is claims, identity, principal, ticket. Thus, it's not a clear process.

    • @DotNetCoreCentral
      @DotNetCoreCentral  4 года назад +1

      Алексей Ш thanks for the feedback. I’ll create a video to do that. Thanks for watching.

    • @syednizamudeen6299
      @syednizamudeen6299 4 года назад

      Yes correct question. there is no clear explanation

  • @csharpcoder9109
    @csharpcoder9109 2 года назад

    Good one here. Thanks

  • @mdrave
    @mdrave 4 года назад

    exactly what I needed, thanks a lot :)

    • @DotNetCoreCentral
      @DotNetCoreCentral  4 года назад

      I’m glad the video helped you. Thanks for watching.

  • @Kryzon69
    @Kryzon69 3 года назад

    Thanks, my friend!!!

  • @wolfgangmena-bruhn5992
    @wolfgangmena-bruhn5992 3 года назад

    Difference of the custom handler?
    I am missing the big picture. What is the purpose or the advantages of the custom handler compared against the implementation showed in the previous video?

    • @wolfgangmena-bruhn5992
      @wolfgangmena-bruhn5992 3 года назад

      Sorry, I realized, that this question was already answered below. Thank you very much.

    • @DotNetCoreCentral
      @DotNetCoreCentral  3 года назад

      @@wolfgangmena-bruhn5992 you are welcome!

  • @christusjesus89
    @christusjesus89 4 года назад

    Hi, Thanks. This Video helps me alot.

  • @ariana4597
    @ariana4597 3 года назад

    Great video! I got a question I would appreciate it if you could answer me. What can we do with the SchemeOptions here?

  • @gshekhar2727
    @gshekhar2727 3 года назад

    Hi DotNet Core Central, I have custom API Exception middleware and added in pipeline app.UseApiExceptionHandler();. I have implemented code as shown in above video, however, when I return AuthenticateResult.Fail it goes to my ExceptionMiddleware and instead of returning Unauthorized it returns Internal Server error. I tried to figure out this behavior, however could not figure this out. Could you please help.

    • @DotNetCoreCentral
      @DotNetCoreCentral  3 года назад +1

      @G Shekhar, I will take a look and try to reproduce the issue and let you know what I find out.

    • @gshekhar2727
      @gshekhar2727 3 года назад

      @@DotNetCoreCentral yes please, thanks

    • @gshekhar2727
      @gshekhar2727 3 года назад

      @DotNet Core Central, I was able to resolve this. App.UseAuthorization was called before app.UseRouting. Would like to know more on Authentication.. could you share your blog/ video link.

    • @DotNetCoreCentral
      @DotNetCoreCentral  3 года назад

      @@gshekhar2727 I am glad that your issue got resolved.
      I have couple of blogs you can take a look:
      dotnetcorecentral.com/blog/asp-net-core-authorization/
      dotnetcorecentral.com/blog/authentication-handler-in-asp-net-core/

  • @mansimandlik9013
    @mansimandlik9013 3 года назад

    How to send Unauthorised status code with custom message for invalid token, I used AuthenticateResult.Fail("Invalid Token") but it's just printing status code unauthorised 401 without message

    • @DotNetCoreCentral
      @DotNetCoreCentral  3 года назад

      @Mansi Mandlik, thanks for watching the video.
      Inside of your controller, you can use and do not use the Authorize attribute in the controller:
      if (!User.Identity.IsAuthenticated)
      return Unauthorized("your message here");

  • @ravindranaths513
    @ravindranaths513 4 года назад

    Hi, at the timeline 16:46, you are adding "Basic" as value for defaultScheme parameter. What is the significance of this?

    • @DotNetCoreCentral
      @DotNetCoreCentral  4 года назад +1

      @Ravindranath S, "Basic" is the name of the authentication scheme. It can be anything you decide on your scheme to be. Ideally, I should be using Basic as the token bearer, instead of the Bearer string that I am using. That was a miss on my part. Since you can technically have multiple Authentication schemes in a single project, the scheme name helps identify how to validate the token. I hope this clarifies your doubt.

    • @ravindranaths513
      @ravindranaths513 4 года назад

      @@DotNetCoreCentral , yes, clarified

    • @DotNetCoreCentral
      @DotNetCoreCentral  4 года назад

      @@ravindranaths513 cool!

  • @engineer.me.108
    @engineer.me.108 3 года назад

    Thanks a lot man.

  • @ravindranaths513
    @ravindranaths513 4 года назад

    Hi, at the timeline 15:06, you are passing AuthenticationTicket object to AuthenticateResult.Success() method. But my doubt is where you are using this added ticket & what is the significance of this ticket?

    • @DotNetCoreCentral
      @DotNetCoreCentral  4 года назад +1

      @Ravindranath S, in the Controller, when you access User object, it internally is populated from this ticket. That is why you can do User.Identity.IsAuthenticated and User.IsInRole etc. User is the ClaimsPrinciple object which is populated from the ticket.

    • @gshekhar2727
      @gshekhar2727 3 года назад

      @@DotNetCoreCentral how to access that User object in controller? could you explain?

    • @DotNetCoreCentral
      @DotNetCoreCentral  3 года назад

      @@gshekhar2727 The user object is set as a part of setting the principal. So you can use Request.HttpContext.User to access user info.

    • @gshekhar2727
      @gshekhar2727 3 года назад

      @@DotNetCoreCentral Thanks, I have a custom logger (Serilog), how do I inject that dependency in my CustomAuthenticationHandler

    • @DotNetCoreCentral
      @DotNetCoreCentral  3 года назад

      @@gshekhar2727 I will suggest just use ILogger from Microsoft.Extensions, and the add Serilog to the logging extension. That way your code just deals with the ILogger extension, whereas in DI you can configure the logging extension to use Serilog.

  • @yotambelgoroski9558
    @yotambelgoroski9558 4 года назад

    Hi, Thanks for the great video
    Is there a way we can have 2 Authenticaion Handlers?
    What I mean is that in 16:30 you remove the JwtBearer authentication handler so you can use your "Basic" handler, but what if you want both?
    I.e. What if you want to check for JWT authentication and if that fails, instead of immidiatly return 401, you'll execute your "Basic" handler and allow for the user to authenticate using it's authentication logic? How would you have done such a thing?

    • @DotNetCoreCentral
      @DotNetCoreCentral  4 года назад

      @Yotam Belgoroski, you can definitely do that. But in that case, your handler needs to be generic and handle one after another. So you should not be using the JWT extension method available out of the box, instead, you will have to handle token validation and extraction yourself. Let me know if that makes sense or if you need more information. Thanks!

  • @deepakbhalode4419
    @deepakbhalode4419 3 года назад

    Thanks for creating such a wonderful content.
    Is there any blog of yours, which talks about when to use custom authentication handler and when to use that authentication handler which you taught in previous video ??

    • @DotNetCoreCentral
      @DotNetCoreCentral  3 года назад +2

      @Deepak Bhalode, thanks for watching! I have a blog site here: dotnetcorecentral.com/
      But I am not sure I have specifically what you are asking for.

    • @deepakbhalode4419
      @deepakbhalode4419 3 года назад

      @@DotNetCoreCentral Thank you.
      Keep growing 🌱

    • @DotNetCoreCentral
      @DotNetCoreCentral  3 года назад

      @@deepakbhalode4419 thanks!

  • @giampaolotucci5897
    @giampaolotucci5897 2 года назад

    Tnx! Great video! Could create a video about Azure AD?

    • @DotNetCoreCentral
      @DotNetCoreCentral  2 года назад

      @Giampaolo TUCCI, thanks for watching! Yes, I will do videos in Azure in near future.