如果帐户过期,则强制用户注销

I'm trying to force the user to logout if his account is expired. I'm using Asp.Net MVC core 3, And here one of the solution I got. I added new class under Services "ValidateAsync". public class ValidateAsync { public static async Task ValidatingAsync(CookieValidatePrincipalContext context) { context = context ?? throw new ArgumentNullException(nameof(context)); var claimsIdentity = context.Principal.Identity as ClaimsIdentity; if (claimsIdentity?.Claims == null || !claimsIdentity.Claims.Any()) { await RejectPrincipal(); return; } UserManager<IdentityUser> userManager = context.HttpContext.RequestServices.GetRequiredService<UserManager<IdentityUser>>(); var user = await userManager.FindByNameAsync(context.Principal.FindFirstValue(ClaimTypes.NameIdentifier)); if (user == null || user.SecurityStamp != context.Principal.FindFirst(new ClaimsIdentityOptions().SecurityStampClaimType)?.Value) { await RejectPrincipal(); return; } async Task RejectPrincipal() { context.RejectPrincipal(); await context.HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); } } } In Startup class I added inside 'ConfigureServices' method: services.ConfigureApplicationCookie(options => { options.Events = new CookieAuthenticationEvents { OnValidatePrincipal = ValidateAsync }; }).Configure<SecurityStampValidatorOptions>(options => { options.ValidationInterval = TimeSpan.Zero; }); And in here I have a background process where it set the user expired, and at the same I want to force him to logout if he is logged in after being expired. public async Task SetExpired() { foreach(var item in _db.Institution) { if (item.SubscriptionEndDate != null) { if (item.SubscriptionEndDate == DateTime.Today) { item.Status = SD.StatusExpired; //Here I want to check if the user is logged in, then force logout should be done. Guid securityStamp = Guid.NewGuid(); item.SecurityStamp = securityStamp; } } } await _db.SaveChangesAsync(); } I don't know if my approach should work as expected or not. But ValidateAsync in startup class show the following error: CS0119: 'ValidateAsync' is a type, which is not valid in the giving context. please any help is much appreciated.


A small misuse. Change options.Events = new CookieAuthenticationEvents { OnValidatePrincipal = ValidateAsync }; to options.Events = new CookieAuthenticationEvents { OnValidatePrincipal = ValidateAsync.ValidatingAsync };