src/AspNetCore/Identity/Bearer/src/Authentication/Utils/TokenValidatedContextUtils.cs
/// <summary>
/// The user may be authenticated but the security stamp could
/// have been changed and therefore not be authenticated anymore.
/// In the claims principal of <paramref name="context"/> we rely on
/// <see cref="ClaimsIdentityOptions.UserIdClaimType"/> and
/// <see cref="ClaimsIdentityOptions.SecurityStampClaimType"/>.
/// Therefore it should be in need of, that
/// <see cref="ValidateRefreshTokenIdClaim(TokenValidatedContext)"/>
/// is called first.
/// </summary>
The user may be authenticated but the security stamp could
have been changed and therefore not be authenticated anymore.
In the claims principal of  we rely on
and
.
Therefore it should be in need of, that


The user may be authenticated but the security stamp could have been changed and therefore not be authenticated anymore . In the claims principal of we rely on and . Therefore it should be in need of that
public static async Task ValidateSecurityStamp(TokenValidatedContext context)
        {
            var signInManager = context.HttpContext.RequestServices.GetRequiredService<SignInManager<UserEntity>>();
            var user = await signInManager.ValidateSecurityStampAsync(context.Principal);
            if (user == null) {
                context.Fail("You are not authenticated anymore.");
            }
        }
public static async Task ValidateSecurityStamp ( TokenValidatedContext context ) { var signInManager = context . HttpContext . RequestServices . GetRequiredService < SignInManager < UserEntity > > ( ) ; var user = await signInManager . ValidateSecurityStampAsync ( context . Principal ) ; if ( user == null ) { context . Fail ( " " ) ; } }
The user may be authenticated but the security stamp could
have been changed and therefore not be authenticated anymore.
The user may be authenticated but the security stamp could have been changed and therefore not be authenticated anymore .
// Why does this works?? IdentityOptions.ClaimIdentity.UserIdClaimType is by default // ClaimTypes.NameIdentifier, and this one will be grabbed, if not user will be null. /// The result of <see cref="TokenValidatedContext.Result"/> will be set. /// And when it is faulty, then this result will be the http response.
context
TokenValidatedContext