JWT Authentication in ASP.NET Core 7 Web API

Поделиться
HTML-код
  • Опубликовано: 4 окт 2024
  • Welcome to this comprehensive RUclips tutorial on implementing JWT (JSON Web Token) Authentication in ASP.NET Core 7 Web API. In this video, we'll guide you through the process of adding a robust and secure authentication mechanism to your API using JWTs.
    JWT Authentication is a popular method for securing APIs due to its compactness, self-contained nature, and efficient validation process. It enables you to transmit user identity, roles, and permissions within a token, making it an excellent choice for modern application architectures.
    Key Takeaways:
    Understanding JWT Authentication: Get a clear understanding of what JWT Authentication is and why it's a popular choice for securing APIs.
    Installing Required Packages: Learn how to install and configure the necessary NuGet packages for JWT Authentication in your ASP.NET Core 7 project.
    Configuring Authentication: Explore how to configure JWT Authentication in the program.cs file using the AddAuthentication method.
    Configuring JWT Options: Set up JWT options such as issuer, audience, signing key, and clock skew to ensure secure token generation and validation.
    Securing Endpoints: Discover how to use the [Authorize] attribute to secure specific endpoints, allowing access only to authenticated users.
    Token Generation: Learn how to generate JWTs after successful user authentication, and understand the structure of a JWT.
    Token Validation: See how the JwtBearer middleware automatically validates JWTs, ensuring the token's integrity and authenticity.
    Testing JWT Authentication: Use tools like Postman to test your JWT Authentication implementation and observe the results.
    Best Practices for JWT Security: Learn about security best practices when using JWTs, including token expiration, refresh tokens, and token storage.
    Decoding JWT Payload: Understand how to decode and interpret the claims contained within a JWT for debugging and auditing purposes.
    By the end of this tutorial, you'll have a solid grasp of implementing JWT Authentication in your ASP.NET Core 7 Web API. Whether you're building a RESTful API for a single-page application or a mobile app, JWT Authentication offers a powerful and flexible way to ensure secure communication between clients and servers.
    Join us on this journey to enhance the security of your .NET 7 Web API. Don't forget to subscribe and hit the notification bell for more insightful tutorials and tech content. Secure your API with confidence-let's get started!
    Click here to see the code on github.
    github.com/tec...
    Basic Authentication using Middleware in ASP.NET Core Web API
    • Unlock the Secrets of ...
    Dependency Injection Service Lifetimes in .NET 7 Web API
    • Master Dependency Inje...
    Dependency Injection in ASP.NET Core (.NET 7) Web API
    • Why You Should Use Dep...
    .Net 7 Web API CRUD Operation using EF Core and SQL Server
    • .Net 7 CRUD Web API us...
    Repository Pattern in .Net 7
    • Repository Pattern in ...
    Generic Repository Pattern in .Net 7
    • Generic Repository Pat...
    Unit of work with in .NET 7
    • Unit of Work in Reposi...
    Entity Framework Core All Entity Relationship Mappings
    • Entity Framework Core ...
    Sorting, Filtering and Pagination in .NET 7 Web API using Sieve
    • Sorting, Filtering and...
    Global Exception Handling in .NET 7 Web API
    • Global Exception Handl...
    .NET 7 Web API CRUD Operations using Dapper
    • How To Use Dapper For ...
    JWT Authentication .NET 7 and .NET 8
    Secure Web API with JWT .NET 8
    Implement JWT in .NET 7 API
    .NET 8 JWT Token Authentication
    JWT Authentication Tutorial .NET 7
    ASP.NET Core JWT Authentication .NET 8
    Secure .NET API with JWT
    Token-based Authentication in .NET 7/8
    JWT Implementation in .NET 8 Web API
    Authentication with JWT in .NET 7 Web API
    #jwt #jsonwebtoken #jwtimplementation #securewebapi #secure
    #authentication #secure #securewbapi #middleware #webapi #dotnet7 #dotnetprojects #dotnetprojectcenters #middleware #techyatra #nishantgupta #efcore #learnprogramming #learncoding #learndotnet
    #dotnet7
    #dotnet8
    #jwt
    #authentication
    #webapi
    #aspnetcore
    #jwtsecurity
    #dotnetcore
    #programming
    #softwaredevelopment

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

  • @MOHITYADAV-vk5xk
    @MOHITYADAV-vk5xk Год назад +7

    Hindi me dot ke liye sabse acchi video ❤

    • @tech_yatra
      @tech_yatra  Год назад +2

      Thanks brother, keep supporting

  • @alisalim723
    @alisalim723 10 дней назад +1

    very very prefect

    • @tech_yatra
      @tech_yatra  9 дней назад

      Thank you for your kind words. ❤️

  • @techyoutube6625
    @techyoutube6625 9 месяцев назад +3

    Very well explained, better than many English channels

    • @tech_yatra
      @tech_yatra  9 месяцев назад

      Thanks for your kind words

  • @soumenmondal38
    @soumenmondal38 24 дня назад +1

    sir your explanation is very workable in my project. Please send me how to inject AES algorithm in this project if possible. It will be very helpful to me thank you

  • @asifjamal-m8g
    @asifjamal-m8g 7 месяцев назад +2

    very informative. excellent

    • @tech_yatra
      @tech_yatra  7 месяцев назад

      Glad it was helpful!

  • @kiran-bobade
    @kiran-bobade 9 месяцев назад +1

    very nicely expalin sir..best on you tube for .net

    • @tech_yatra
      @tech_yatra  9 месяцев назад

      Thanks for your kind words. 🙏❤️😊

  • @Aman-ih8ub
    @Aman-ih8ub Месяц назад +2

    Very good Explanation!

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

    Excellent explanation 🎉

    • @tech_yatra
      @tech_yatra  5 месяцев назад

      🙏 Thanks 👍

    • @ketanparab7462
      @ketanparab7462 5 месяцев назад +1

      @@tech_yatra thank you for your reply sir. Your explanation is very easy to understand. One request to you, Could you pleae make video on microservices in c# like
      API gateway
      Authorization and authentication microservices
      Comunicate microservices each other

    • @tech_yatra
      @tech_yatra  5 месяцев назад

      Sure, I will make.

  • @josbexerra8115
    @josbexerra8115 3 месяца назад +1

    muchas gracias Mister muy bien explicado......

    • @tech_yatra
      @tech_yatra  3 месяца назад

      Me alegro que te guste esto. muchas gracias por tus amables palabras.

  • @AnshuRaj12991
    @AnshuRaj12991 4 дня назад +2

    Bro mera token swagger pe nhi aa rha hai but mai debugger lagaakar check kar rha hu to generate ho rha hai can you explain why did happen bro mai 2 din se try kar rha hu but swagger pe show hi nhi ho rha hai token😢😢

  • @pwn16
    @pwn16 9 месяцев назад +1

    nice keep uploading videos

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

    Well explained viseo, could you please make Refresh JWT Token video also.
    Or if you already make please share video link.
    Thanks

    • @tech_yatra
      @tech_yatra  24 дня назад +1

      Sure this is in my list

  • @SanjaySharma-s2d
    @SanjaySharma-s2d 8 месяцев назад +2

    Well explained, could you please explain it using stored procedure and password hashing

    • @tech_yatra
      @tech_yatra  8 месяцев назад

      Sure I will make a video on this soon.

  • @mohrahulrathi3669
    @mohrahulrathi3669 Месяц назад +2

    Thanks

  • @franciscoprazzio225prazzio
    @franciscoprazzio225prazzio Год назад +2

  • @akc1308
    @akc1308 6 месяцев назад +1

    nice tutorial
    🤘

    • @tech_yatra
      @tech_yatra  6 месяцев назад +1

      Thanks 🙏👍 kindly like share and subscribe.

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

    How do we validate this token i mean what if we make change in this generated token how we know that this is not the correct token

  • @ketanparab7462
    @ketanparab7462 5 месяцев назад +1

    Could you please create video on refresh token?

    • @tech_yatra
      @tech_yatra  5 месяцев назад

      Sure I will create

  • @pieceofcode_
    @pieceofcode_ 10 дней назад +1

    good explanation.
    I have a query regarding where to use?
    1. If we use gRPC, are we not repeating model information? one in our .NET or Java application and same model in proto buff file?
    2. If I use in in Authentication api, so is flow like below
    Ocelot -> Authentication Microservice -> gRPC client code -> gRPC server ?

    • @tech_yatra
      @tech_yatra  9 дней назад

      Thanks for your question!
      Yes, in gRPC, the model is defined twice - once in your .NET/Java app and once in the protobuf file. The protobuf helps in fast and efficient communication between services, even if it seems like repetition.
      Your flow looks correct: Ocelot -> Authentication Microservice -> gRPC client -> gRPC server.

  • @kakdiyaamit6625
    @kakdiyaamit6625 6 месяцев назад

    hum Userwise database create karke. connection string change kar sakte hai login time par?

  • @asifjamal-m8g
    @asifjamal-m8g 7 месяцев назад +1

    Hi Nishant how r u. pls help Employee data which we are getting by consuming this api as in video and HOW we post it into another api Employee table whose column are different from out db employee column? how we mapp out employeeModel and other api employeeModel.

    • @tech_yatra
      @tech_yatra  7 месяцев назад +1

      To map Employee data to another API with different columns, use a mapping library like AutoMapper. Define a mapping between your EmployeeModel and the target API's EmployeeModel.
      After mapping, use HttpClient to send a POST request to the other API with the mapped data.
      Feel free to ask if you have more questions!

  • @poojatarge2580
    @poojatarge2580 7 месяцев назад +1

    How to generate key dynamically.. here we are hardcoding which is not secure

    • @tech_yatra
      @tech_yatra  7 месяцев назад

      Thanks for your comment! Generating keys dynamically for Jwt authentication in .NET 7 Web API is a great consideration for improved security. To achieve this dynamically, you can explore using a secure key generation mechanism, perhaps leveraging libraries like System.Security.Cryptography.
      I appreciate your emphasis on security, and I'll definitely consider creating content on dynamic key generation in an upcoming video. Feel free to subscribe for updates!
      If you have any more questions or suggestions, please let me know.

  • @MuhammadAliSherserve
    @MuhammadAliSherserve 5 месяцев назад

    how you created jwt key, issuer, audience and subject json?

    • @tech_yatra
      @tech_yatra  4 месяца назад

      The values for the audience, issuer, and subject in a JWT are typically determined based on your application's context and requirements
      Issuer (iss): "my-app"
      Audience (aud): "my-app-users"
      Subject (sub): "user123"
      Values:
      Issuer (iss): "my-app"
      Audience (aud): "my-app-users"
      Subject (sub): "user123"
      Code:
      javascript
      Copy code
      const jwt = require('jsonwebtoken');
      const payload = {
      sub: 'user123',
      iss: 'my-app',
      aud: 'my-app-users',
      exp: Math.floor(Date.now() / 1000) + (60 * 60)
      };
      const secretKey = 'your-secret-key';
      const token = jwt.sign(payload, secretKey);
      console.log(token);

  • @user-et6wu8vr9y
    @user-et6wu8vr9y 8 месяцев назад +1

    Hello Nishant, Very well explained , thanks for this. Can you please tell me how can we get the Jwt to configure in Appsettings.json file, how did you get that key ,issuer and audience?

    • @tech_yatra
      @tech_yatra  7 месяцев назад

      This information will be used later to generate a JSON Web Token. Note that you can give any name to this section you want. You can use www.random.org/strings to generate random strings. Make sure you are not doing this for production

    • @tech_yatra
      @tech_yatra  7 месяцев назад

      If you have any other questions feel free to ask. Thank you

    • @user-et6wu8vr9y
      @user-et6wu8vr9y 7 месяцев назад

      @@tech_yatra : Yes please, I want to know how you got this values of Key, Issuer, Audience. Please guide me. Its very grateful you saw my message and responding.
      "Jwt": {
      "Key": "Yh2k7QSu4l8CZg5p6X3Pna9L0Miy4D3Bvt0JVr87UcOj69Kqw5R2Nmf4FWs03Hdx",
      "Issuer": "JWTAuthenticationServer",
      "Audience": "JWTServicePostmanClient",
      "Subject": "JWTServiceAccessToken"
      }

    • @ayushchourasiya4616
      @ayushchourasiya4616 3 месяца назад

      @@user-et6wu8vr9y Have you get this solution how did he get that values of key, issuer, audience...???

    • @user-et6wu8vr9y
      @user-et6wu8vr9y 3 месяца назад

      @@ayushchourasiya4616nope, Those are random itseems

  • @ashishingle9298
    @ashishingle9298 4 месяца назад +1

    Authentication hone ke bad jo JWT token generate hoga use kaha store karna chahiye frontend me take next time protected API ko call karte time use kar sake... Please help me to get answer for this.

    • @tech_yatra
      @tech_yatra  4 месяца назад +1

      JWT token ko securely store karne ke liye localStorage, sessionStorage, ya httpOnly cookies use kar sakte hain. HttpOnly cookies sabse secure hain kyunki ye XSS attacks se bachata hai. Hope this helps.

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

      JWT token ko front-end me store karne ke liye, aap localStorage, sessionStorage, ya httpOnly cookies ka istemal kar sakte hain.localStorage aur sessionStorage JavaScript se accessible hote hain jabki httpOnly cookies XSS attacks se bachate hain.

    • @ashishingle9298
      @ashishingle9298 4 месяца назад

      @@tech_yatra thanks a lot for replying. Agar ho sake to ek short video is topic pe bhi bana dijiye is topic pe clear information wali video nahi hai. 🙏🏻♥️

  • @chinmaykshah
    @chinmaykshah 7 месяцев назад +1

    very informative. can you try to make video on auth0?

    • @tech_yatra
      @tech_yatra  7 месяцев назад +2

      Sure, the next video is on auth0, and will be uploaded on the weekend.
      Thanks for your kind words 🙏☺️

    • @chinmaykshah
      @chinmaykshah 7 месяцев назад

      @@tech_yatra Hi,when are you planning to upload auth0 video?

  • @SanjaySharma-s2d
    @SanjaySharma-s2d 8 месяцев назад +1

    Please explain how to consume this web api

  • @mindset1ark
    @mindset1ark 8 месяцев назад +1

    Please your make video
    How to make forget password in asp .net core web api in user?

    • @tech_yatra
      @tech_yatra  8 месяцев назад

      Sure I will add this to my list, you will get it soon.

  • @atulbhandari2786
    @atulbhandari2786 8 месяцев назад +1

    Hello sir .token jb expire hota h to phir vo generate kaise hota h ?

    • @tech_yatra
      @tech_yatra  8 месяцев назад

      Jab JWT token expire ho jata hai, server-side logic naya token generate karta hai. Yeh process server policies aur client interaction par depend karta hai. Agar kuch aur poochna hai, feel free to ask!

    • @atulbhandari2786
      @atulbhandari2786 8 месяцев назад +1

      Like agr mai continuously work kr rha hu...token expire time 20 minutes.after 20 minutes kya hoga?

    • @tech_yatra
      @tech_yatra  7 месяцев назад +1

      Refresh tokens are the kind of tokens that can be used to get new access tokens. When the access tokens expire, we can use refresh tokens to get a new access token from the authentication controller.

    • @atulbhandari2786
      @atulbhandari2786 7 месяцев назад +1

      Thank you sir

    • @atulbhandari2786
      @atulbhandari2786 7 месяцев назад

      Sir I have one question regarding dot net ...how to convert database table result to image format in aap net core web api ?

  • @debajyotimohanty7613
    @debajyotimohanty7613 3 месяца назад +1

    Bhai iska database ka script mil sakta hai kya please?

    • @tech_yatra
      @tech_yatra  3 месяца назад

      I do not have a db script. You can do the migration as I did by following the same steps.

  • @vivekkanhekar1991
    @vivekkanhekar1991 6 месяцев назад +1

    Postman me data Get nhi hora h jab test kiya toh

    • @tech_yatra
      @tech_yatra  6 месяцев назад

      Agar koi issue hai aap mail pe details send kr dijiye me check krke btata hu

    • @vivekkanhekar1991
      @vivekkanhekar1991 6 месяцев назад +1

      Mail Id?@@tech_yatra

    • @tech_yatra
      @tech_yatra  6 месяцев назад

      thetechyatra@gmail.com

    • @vivekkanhekar1991
      @vivekkanhekar1991 6 месяцев назад

      @@tech_yatra I sent you my issue on your mail id

  • @RAHULYADAV-ck4vk
    @RAHULYADAV-ck4vk 3 месяца назад +1

    your video are very informative,but I contact with you..plz reply bro🙏

    • @tech_yatra
      @tech_yatra  3 месяца назад

      I am glad you like my videos. Thanks for your kind words ☺️🙏

    • @RAHULYADAV-ck4vk
      @RAHULYADAV-ck4vk 3 месяца назад +1

      @@tech_yatra bro Aapse kaise contact kr skte hai..plz reply back🙏

    • @tech_yatra
      @tech_yatra  3 месяца назад

      Aap thi pe bta dijiye ......ki kya puchna hai aapko

  • @firdaushalam2067
    @firdaushalam2067 4 месяца назад +1

    Can I talk to you by mob.

    • @tech_yatra
      @tech_yatra  4 месяца назад

      Could you please write here ?

  • @ersuresh4488
    @ersuresh4488 3 месяца назад

    Jo main part h es video ka usko sirf copy paste Kiya h no explanation.. so no use of watching this video

    • @tech_yatra
      @tech_yatra  3 месяца назад

      Konsa part aapko samajh nhi aaya mujhe btao ...I will explain you

  • @AnandRaj-hq4zv
    @AnandRaj-hq4zv 5 месяцев назад

    Can you share code?

    • @tech_yatra
      @tech_yatra  5 месяцев назад

      Kindly check the video description...you will find the repo url there.