JWT Authentication validation in asp.net core

I have followed this link (Token Based Authentication in ASP.NET Core) to create the jwt token and I am successful to create the token

Below is the image

令牌创建

Now I want to validate the token I have passed the token as below

令牌验证

I am getting 401 authorization.Please let me know the where I am doing wrong.

I have taken the same code as mention in the blog

Here is the code link

https://github.com/mrsheepuk/ASPNETSelfCreatedTokenAuthExample


Your problem is this line in TokenController.cs :

 var handler = new JwtSecurityTokenHandler();

you can't just instantiate a new handler for every request. You need to use an handler created using your JwtBearerOptions - when you just instantiate you don't use the signingKey you placed in Startup.cs

public TokenController(IOptions<JwtBearerOptions> options)
{
    _bearerOptions = options.Value;
}

and in GetToken

JwtSecurityTokenHandler handler = _bearerOptions.SecurityTokenValidators.OfType<JwtSecurityTokenHandler>().First();
链接地址: http://www.djcxy.com/p/22388.html

上一篇: ASP.NET 5创建RSA密钥,

下一篇: 在asp.net核心中进行JWT身份验证验证