Introduction
In this article, I am going to demonstrate how to set up a back-end application to validate a client request through tokenization and secure an endpoint to be accessible only by a valid token.
I am going to use JWT (JSON Web Token) as a tokenization standard for authentication.
JWT is a very popular and widely used tokenization standard. It is an open standard (RFC 7519) and the information received by this is trusted as it is digitally signed.
I will be using .NET Core 2.1 for API framework.
Background
As you know, token-based authentication is trendy these days. Is it worth it? To answer this question, we need to understand the concept of cookie-based authentication.
A cookie-based authentication requires a client to provide valid credentials (username and password). The server validates it and creates a session and sends the session id to the client (browser). The server also saves this session id in its repository so that it can compare for each incoming request the next time. The client saves this session id as cookie and attaches it with resource request to the server.

The token-based method overcomes the shortcomings of cookie-based authentication.
In token-based authentication, a client is given token instead of a cookie. The tokens are light-weight JSON (JavaScript Object Notation) and contain encoded information about the user and expiry time.
JWT (JSON Web Token) is a very common format of token-based implementation. It is so popular right now that it has become a de-facto standard for token-based authentication.
JWT is composed of three components, separated by a dot (.)

- {
- "alg": "HS256",
- "typ": "JWT"
- }
- {
- “issuer”: “http://www.exampleweb.com”,
- “expires”: “2015-11-18T18:25:43.511Z”
- }

Implementation




- public void Configure(IApplicationBuilder app, IHostingEnvironment env)
- {
- .
- .
- .
- app.UseAuthentication();
- app.UseMvc();
- }








Lawrence PondPosted Oct 11, 2021, 9:22 PM
Tokens provide a stateless authentication mechanism, removing the need for servers to store a record of logged-in users as opposed to cookies.
sayyad hasanPosted Jun 29, 2021, 11:39 AM
Thanks Dear Dey, excellent explanation
M ISPosted Feb 9, 2021, 12:22 PM
Does token validate and decode as automatically in server after client send the token?
Praveen KumarPosted Aug 8, 2019, 7:52 AM
Thank you chinmay its very clean to understand will you please explain Mapper in core
Michael WhittPosted Jun 26, 2019, 11:56 AM
Hi Chinmay. Thanks for the tutorial. What should be substituted for "KeyForSignInSecret@1234" or how can that key be generated?
MichaelPosted Jun 12, 2019, 7:35 AM
Good...the actual port number you test in postman could be different, you can lookup or set the port number at Properties/launchSettings.json file. Also, you need to select correct verb in postman: POST http://localhost:65321/api/auth/login and GET http://localhost:65321/api/values
Sri HarshaPosted Mar 15, 2019, 2:07 AM
Very clearly explained article :) Thank you