Sumit Singh
Sumit Singh

Reputation: 59

implementing authentication and authorization using JWT token

I am just confuse to use UseJwtBearerAuthentication or JWT with OAUTH 2 for implementing authentication and authorization in .Net Core Web API.

Upvotes: 1

Views: 262

Answers (1)

Win
Win

Reputation: 62300

JSON Web Token (JWT) is an implementation of OAUTH 2.

ASP.NET Core and Angular 2 by Valerio De Sanctis explains very detail on how to implement JWT provider in ASP.NET Core. Here is the sample code -

public void Configure(IApplicationBuilder app...)
{
     ...
    // Add a custom Jwt Provider to generate Tokens
    app.UseJwtProvider();

    // Add the Jwt Bearer Header Authentication to validate Tokens
    app.UseJwtBearerAuthentication(new JwtBearerOptions()
    {
        AutomaticAuthenticate = true,
        AutomaticChallenge = true,
        RequireHttpsMetadata = false,
        TokenValidationParameters = new TokenValidationParameters()
        {
            IssuerSigningKey = JwtProvider.SecurityKey,
            ValidateIssuerSigningKey = true,
            ValidIssuer = JwtProvider.Issuer,
            ValidateIssuer = false,
            ValidateAudience = false
        }
    });
    ...
}

Unfortunately, the sample code is written in previous version of ASP.NET Core which used project.json. I convert the project to csproj at GitHub.

Upvotes: 2

Related Questions