What is JSON Web Token?

JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with the HMAC algorithm) or a public/private key pair using RSA or ECDSA.

Although JWTs can be encrypted to also provide secrecy between parties, we will focus on signed tokens. Signed tokens can verify the integrity of the claims contained within it, while encrypted tokens hide those claims from other parties. When tokens are signed using public/private key pairs, the signature also certifies that only the party holding the private key is the one that signed it.

How to Setup Token

Open Startup.cs Class. At Method ConfigureServices add below code :

Code : 

var appSettingsSection = Configuration.GetSection("AppSettings");

            services.Configure<AppSettings>(appSettingsSection);

             var appSettings = appSettingsSection.Get<AppSettings>();

            var key = Encoding.ASCII.GetBytes(appSettings.Secret);

            services.AddAuthentication(x =>

            {

                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;

                x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;

            })

            .AddJwtBearer(x =>

            {

                x.RequireHttpsMetadata = false;

                x.SaveToken = true;

                x.TokenValidationParameters = new TokenValidationParameters

                {

                    ClockSkew = TimeSpan.FromMinutes(30),

                    ValidateIssuerSigningKey = true,

                    IssuerSigningKey = new SymmetricSecurityKey(key),

                    ValidateIssuer = false,

                    ValidateAudience = false

                };

            });


                                 Full Video Tutorial

    

                 How to Create Token

var tokenHandler = new JwtSecurityTokenHandler();

            var key = Encoding.ASCII.GetBytes(_appSettings.Secret);

            var tokenDescriptor = new SecurityTokenDescriptor

            {

                Subject = new ClaimsIdentity(new Claim[]

                {

                    new Claim(ClaimTypes.Name, user.UserInfoId.ToString())

                }),

                Expires = DateTime.UtcNow.AddDays(7),

                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)

            };

            var token = tokenHandler.CreateToken(tokenDescriptor);

            user.Token = tokenHandler.WriteToken(token);




 Full Video Tutorial


                      How to Use Token


VIDEO TUTORIALS YOU MAY LIKE

1. JWT Auth ASP.NET Core : 

https://www.youtube.com/watch?v=leEMNOwCuUM


2. Angular Auth with ASP.NET Core : 

https://www.youtube.com/watch?v=UhIrOn5Gsp8


3. Create and Use Session ASP.NET Core : 

https://www.youtube.com/watch?v=AK_lllhY9u4


4. Generate and Download PDF in ASP.NET Core : 

https://www.youtube.com/watch?v=SLNAKXd0NvM


Popular Tutorial Playlists

 ✔️ All CRUD operations (https://bit.ly/2kALx1I

✔️ Xamarin Tutorial series (https://bit.ly/2m4Wycb

✔️ ASP.NET Core (https://bit.ly/2kai5zE

✔️ BLAZOR Tutorial (https://bit.ly/2vFMfA8

✔️ ASP.Net MVC (https://bit.ly/2kyaQBy

✔️ Web API (https://bit.ly/2m0dBMl

✔️ Entity Framework (https://bit.ly/2kAuDjP

✔️ Reporting (https://bit.ly/2kB61Yn

✔️ SQL Server (https://bit.ly/2k1hb8m

✔️ Angular 8 (https://bit.ly/31XPGfZ

✔️ Angular 4 (https://bit.ly/2m6ZTaL

✔️ Angular 2 (https://bit.ly/2k52DVh

✔️ AngularJS (https://bit.ly/2m5VZil

✔️ C# Desktop app (https://bit.ly/2lDvPDk

✔️ LINQ (https://bit.ly/2kALy5N