Search code examples
asp.net-coreopenid-connectopeniddict

Retrieving the time that an access token was generated?


I'm trying to get the claim that contains the token creation date... I've tried the following:

var createdDate = contextAccessor.HttpContext.User.FindFirstValue("IssuedUtc");

var createdDate = contextAccessor.HttpContext.User.FindFirstValue("iss");

var createdDate = contextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.AuthenticationInstant);

Solution

  • When using JWT as the access token format, you can use the iat claim.

    When using the default opaque format, the creation date is not stored as a claim, but as an authentication property. You can retrieve it by using the AuthenticationManager API:

    var result = await contextAccessor.HttpContext.Authentication.GetAuthenticateInfoAsync(OAuthValidationDefaults.AuthenticationScheme);
    var date = result.Properties.IssuedUtc;
    

    Note: the second option also works with JWT, just replace the OAuthValidationDefaults.AuthenticationScheme constant by JwtBearerDefaults.AuthenticationScheme.