Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of JwtBearerOptions.SaveToken property in ASP.NET Core 2.0+?

The Microsoft Docs just have this description:

Defines whether the bearer token should be stored in the AuthenticationProperties after a successful authorization.

I wondered if saving the JWT allows you to revoke it somehow, but every place I read about JWTs says they are irrevocable. What would you do with a JWT being stored in the AuthenticationProperties?

like image 865
Josh Withee Avatar asked Jul 16 '19 12:07

Josh Withee


People also ask

What is AddJwtBearer?

AddJwtBearer(AuthenticationBuilder) Enables JWT-bearer authentication using the default scheme AuthenticationScheme. JWT bearer authentication performs authentication by extracting and validating a JWT token from the Authorization request header.

What is token in ASP net Core?

JSON Web Tokens (commonly known as JWT) is an open standard to pass data between client and server, and enables you to transmit data back and forth between the server and the consumers in a secure manner.

What is token in. net?

Token-based authentication is a process where the user sends his credential to the server, server will validate the user details and generate a token which is sent as response to the users, and user store the token in client side, so client do further HTTP call using this token which can be added to the header and ...


1 Answers

Storing the JWT in the AuthenticationProperties allows you to retrieve it from elsewhere within your application.

For example, use GetTokenAsync inside of an action, like this:

public async Task<IActionResult> SomeAction()
{
    // using Microsoft.AspNetCore.Authentication;
    var accessToken = await HttpContext.GetTokenAsync("access_token");

    // ...
}

This is useful if, for example, you want to forward the JWT in an outgoing request.

like image 155
Kirk Larkin Avatar answered Sep 17 '22 17:09

Kirk Larkin