I have implemented Identity Core on my .Net5 application to handle login, logout, register and refresh with 4 endpoints.
When I call the logout endpoint and send the refresh token it deletes the refresh token from the database so the user can't refresh the access token. The problem is that I can still use the access token to call the endpoints of my application and obtain the authorization until its regular expiration.
I'd like to know if there is a way to invalidate the access token after the logout without waiting for the expiration.
This is my ConfigureService on the Startup:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "AuthenticationAndAuthorizationAPI", Version = "v1" });
});
services.AddIdentityCore<User>(o =>
{
o.User.RequireUniqueEmail = true;
o.Password.RequireDigit = false;
o.Password.RequireNonAlphanumeric = false;
o.Password.RequireUppercase = false;
o.Password.RequiredLength = 0;
}).AddEntityFrameworkStores<AuthenticationDbContext>();
AuthenticationConfiguration authenticationConfiguration = new();
_configuration.Bind("Authentication", authenticationConfiguration);
services.AddSingleton(authenticationConfiguration);
services.AddEntityFrameworkNpgsql().AddDbContext<AuthenticationDbContext>(options =>
options.UseNpgsql(_configuration.GetConnectionString("DBConnection")));
services.AddSingleton<AccessTokenGenerator>();
services.AddSingleton<RefreshTokenGenerator>();
services.AddSingleton<RefreshTokenValidator>();
services.AddScoped<Authenticator>();
services.AddSingleton<TokenGenerator>();
services.AddScoped<IRefreshTokenRepository, DatabaseRefreshTokenRepository>();
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(o =>
{
o.TokenValidationParameters = new TokenValidationParameters()
{
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(authenticationConfiguration.AccessTokenSecret)),
ValidIssuer = authenticationConfiguration.Issuer,
ValidAudience = authenticationConfiguration.Audience,
ValidateIssuerSigningKey = true,
ValidateIssuer = true,
ValidateAudience = true,
ClockSkew = TimeSpan.Zero
};
});
}