I have Asp.Net Core 2.2 MVC web application in which database calls are handled through Asp.Net Core Web Api 2.2 and this Web API will generates the JWT token post verified the Login credentials and returns back to the MVC application with the JWT token.
In Asp.Net core MVC application Controllers decorated with Authorize attribute to validate subsequent request comes from the browser but here i'm not able to validate the JWT token.
So please suggest how to validate the JWT token in Asp.Net Core 2.2 MVC Web Application.
Thanks in advance!
Code:
services.AddAuthentication(j =>
{
j.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
j.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
j.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x =>
{
x.SaveToken = true;
x.RequireHttpsMetadata = true;
x.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
//ValidateLifetime = true,
//ValidateIssuerSigningKey = true,
ValidIssuer = "xyz.com",
ValidAudience = "xyz.com",
IssuerSigningKey = new SymmetricSecurityKey(key),
ClockSkew = TimeSpan.FromMinutes(5)
};
});
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Login}/{action=Login}/{id?}");
});