src/AspNetCore/Identity/Bearer/src/Authentication/Utils/TokenValidatedContextUtils.cs
/// <summary>
/// Validates whether <see cref="BearerSignInManagerDefaults.BearerSignInManagerRefreshTokenIdClaimType"/> does exist
/// and if so, it does add an user related identity to the claims principal of <paramref name="tokenValidatedContext"/>.
/// </summary>
Validates whether  does exist
and if so, it does add an user related identity to the claims principal of .
Validates whether does exist and if so it does add an user related identity to the claims principal of .
public static async Task ValidateRefreshTokenIdClaim<BearerTokenType>(TokenValidatedContext tokenValidatedContext)
            where BearerTokenType : class, IBearerTokenEntity
        {
            var bearerTokenStore = tokenValidatedContext.HttpContext.RequestServices.GetRequiredService<IBearerTokenStore<BearerTokenType>>();
            var identityOptions = tokenValidatedContext.HttpContext.RequestServices.GetRequiredService<IOptions<IdentityOptions>>();
            try {
                if (tokenValidatedContext.Principal is null) {
                    throw new InvalidOperationException("The principal was expected to be not-null.");
                }
                var refreshTokenEntity = await BearerSignInManagerUtils.FindRefreshTokenAsync(bearerTokenStore, tokenValidatedContext.Principal)
                    ?? throw new InvalidOperationException($"The refresh token identifier is not deposited.");
                var claims = new[] { new Claim(identityOptions.Value.ClaimsIdentity.UserIdClaimType, refreshTokenEntity.UserId.ToString()) };
                var identity = new ClaimsIdentity(claims);
                tokenValidatedContext.Principal.AddIdentity(identity);
            } catch (Exception error) {
                tokenValidatedContext.Fail(error);
            }
        }
public static async Task ValidateRefreshTokenIdClaim < BearerTokenType > ( TokenValidatedContext tokenValidatedContext ) where BearerTokenType : class , IBearerTokenEntity { var bearerTokenStore = tokenValidatedContext . HttpContext . RequestServices . GetRequiredService < IBearerTokenStore < BearerTokenType > > ( ) ; var identityOptions = tokenValidatedContext . HttpContext . RequestServices . GetRequiredService < IOptions < IdentityOptions > > ( ) ; try { if ( tokenValidatedContext . Principal is null ) { throw new InvalidOperationException ( " " ) ; } var refreshTokenEntity = await BearerSignInManagerUtils . FindRefreshTokenAsync ( bearerTokenStore , tokenValidatedContext . Principal ) ?? throw new InvalidOperationException ( $" The refresh token identifier is not deposited. " ) ; var claims = new [ ] { new Claim ( identityOptions . Value . ClaimsIdentity . UserIdClaimType , refreshTokenEntity . UserId . ToString ( ) ) } ; var identity = new ClaimsIdentity ( claims ) ; tokenValidatedContext . Principal . AddIdentity ( identity ) ; } catch ( Exception error ) { tokenValidatedContext . Fail ( error ) ; } }
Validates whether  does exist
and if so, it does add an user related identity to the claims principal of .
Validates whether does exist and if so it does add an user related identity to the claims principal of .
// We add a user related identity to the claims principal.
tokenValidatedContext
TokenValidatedContext