JWT Authentication in ASP.NET Core 7 Web API

Поделиться
HTML-код
  • Опубликовано: 20 авг 2023
  • 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/techyatra/JWT-Auth...
    Basic Authentication using Middleware in ASP.NET Core Web API
    • Basic Authentication u...
    Dependency Injection Service Lifetimes in .NET 7 Web API
    • Dependency Injection S...
    Dependency Injection in ASP.NET Core (.NET 7) Web API
    • Dependency Injection i...
    .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 #jsonwebtoken #jwtimplementation #securewebapi #secure
    #authentication #secure #securewbapi #middleware #webapi #dotnet7 #dotnetprojects #dotnetprojectcenters #middleware #techyatra #nishantgupta #efcore #learnprogramming #learncoding #learndotnet

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

  • @MOHITYADAV-vk5xk
    @MOHITYADAV-vk5xk 10 месяцев назад +5

    Hindi me dot ke liye sabse acchi video ❤

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

      Thanks brother, keep supporting

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

    Very well explained, better than many English channels

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

      Thanks for your kind words

  • @josbexerra8115
    @josbexerra8115 14 дней назад +1

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

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

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

  • @user-zm5oi3ki7h
    @user-zm5oi3ki7h 4 месяца назад +2

    very informative. excellent

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

      Glad it was helpful!

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

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

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

      Thanks for your kind words. 🙏❤️😊

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

    Excellent explanation 🎉

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

      🙏 Thanks 👍

    • @ketanparab7462
      @ketanparab7462 2 месяца назад +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  2 месяца назад

      Sure, I will make.

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

    nice keep uploading videos

  • @franciscoprazzio225prazzio
    @franciscoprazzio225prazzio 10 месяцев назад +2

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

    nice tutorial
    🤘

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

      Thanks 🙏👍 kindly like share and subscribe.

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

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

  • @user-im4vp7jp1z
    @user-im4vp7jp1z 5 месяцев назад +2

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

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

      Sure I will make a video on this soon.

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

    Could you please create video on refresh token?

  • @user-zm5oi3ki7h
    @user-zm5oi3ki7h 4 месяца назад +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  4 месяца назад +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!

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

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

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

      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 4 месяца назад +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  4 месяца назад

      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  4 месяца назад

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

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

      @@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 9 дней назад

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

    • @user-et6wu8vr9y
      @user-et6wu8vr9y 9 дней назад

      @@ayushchourasiya4616nope, Those are random itseems

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

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

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

      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.

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

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

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

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

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

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

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

    Please explain how to consume this web api

  • @ashishingle9298
    @ashishingle9298 Месяц назад +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  Месяц назад +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  Месяц назад +1

      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 Месяц назад

      @@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. 🙏🏻♥️

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

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

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

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

  • @debajyotimohanty7613
    @debajyotimohanty7613 13 дней назад +1

    Bhai iska database ka script mil sakta hai kya please?

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

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

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

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

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

      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 5 месяцев назад +1

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

    • @tech_yatra
      @tech_yatra  4 месяца назад +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 4 месяца назад +1

      Thank you sir

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

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

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

    Postman me data Get nhi hora h jab test kiya toh

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

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

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

      Mail Id?@@tech_yatra

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

      thetechyatra@gmail.com

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

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

  • @RAHULYADAV-ck4vk
    @RAHULYADAV-ck4vk 17 дней назад +1

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

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

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

    • @RAHULYADAV-ck4vk
      @RAHULYADAV-ck4vk 17 дней назад +1

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

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

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

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

    Can I talk to you by mob.

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

      Could you please write here ?

  • @ersuresh4488
    @ersuresh4488 19 дней назад

    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  19 дней назад

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

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

    Can you share code?

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

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