C# 为什么安全戳不注销用户?

C# 为什么安全戳不注销用户?,c#,asp.net,authentication,asp.net-identity,C#,Asp.net,Authentication,Asp.net Identity,我正在使用ASP.NET标识。当我阻止一个用户帐户时,它应该立即注销。 这是我的代码: await UserManager.SetLockoutEnabledAsync(user.Id, true); await UserManager.SetLockoutEndDateAsync(user.Id,DateTime.Today.AddYears(10)); await UserManager.UpdateSecurityStampAsync(user.Id); 在Startup.Auth.cs

我正在使用ASP.NET标识。当我阻止一个用户帐户时,它应该立即注销。 这是我的代码:

await UserManager.SetLockoutEnabledAsync(user.Id, true);
await UserManager.SetLockoutEndDateAsync(user.Id,DateTime.Today.AddYears(10));
await UserManager.UpdateSecurityStampAsync(user.Id);
Startup.Auth.cs
中:

app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login"),
                Provider = new CookieAuthenticationProvider
                {
                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<AppUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromSeconds(0),
                    regenerateIdentity: (manager, user) => manager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie))
                }
            });
app.UseCookieAuthentication(新的CookieAuthenticationOptions
{
AuthenticationType=DefaultAuthenticationTypes.ApplicationOkie,
LoginPath=新路径字符串(“/Account/Login”),
Provider=新CookieAuthenticationProvider
{
OnValidateIdentity=SecurityStampValidator.OnValidateIdentity(
validateInterval:TimeSpan.FromSeconds(0),
regenerateIdentity:(管理员,用户)=>manager.CreateIdentityAsync(用户,DefaultAuthenticationTypes.ApplicationOkie))
}
});

但它不会注销用户。我做错了什么?

为什么不使用
AuthenticationManager.SignOut()


为什么不使用AuthenticationManager.SignOut()


身份验证与cookie连接,并且身份验证信息保存在该cookie上。cookie在您设置的时间内有效,无论您做什么,此cookie都会让用户登录,直到过期或从浏览器中删除

您可以从用户浏览器中删除该cookie,但如果出于某种原因他保留了该cookie,则仍然可以登录,直到cookie过期。因此,如果您的用户在cookie过期之前已经通过身份验证,则实际上已登录

如果您希望立即注销,您需要不时使用ajax进行一些检查,,如果您使用ajax,或在每次页面调用中检查用户的身份验证,或在保存身份验证cookie的数据库上创建一些其他表,并且只需标记那些不再有效的,并且对每个调用进行检查


很抱歉,我没有代码向您展示,这是一个复杂的问题,需要从您的部件设计以满足您的需要和您的程序

身份验证与cookie连接,并且身份验证信息保存在该cookie上。cookie在您设置的时间内有效,无论您做什么,此cookie都会让用户登录,直到过期或从浏览器中删除

您可以从用户浏览器中删除该cookie,但如果出于某种原因他保留了该cookie,则仍然可以登录,直到cookie过期。因此,如果您的用户在cookie过期之前已经通过身份验证,则实际上已登录

如果您希望立即注销,您需要不时使用ajax进行一些检查,,如果您使用ajax,或在每次页面调用中检查用户的身份验证,或在保存身份验证cookie的数据库上创建一些其他表,并且只需标记那些不再有效的,并且对每个调用进行检查

很抱歉,我没有代码向您展示,这是一个复杂的问题,需要从您的部件设计以满足您的需要和您的程序

以下是我的解决方案:

不要调用
UserManager.UpdateSecurityStampAsync(userId)
,而是手动设置安全戳,它将注销用户

public void UpdateSecurityStamp(string userId)
        {
            using (var db = new ApplicationDbContext())
            {
                var user = db.Users.FirstOrDefault(x => x.Id.Equals(userId));
                if (user != null)
                {
                    user.SecurityStamp = Convert.ToString(Guid.NewGuid());
                    db.SaveChanges();
                }
            }
        }
以下是我的解决方案:

不要调用
UserManager.UpdateSecurityStampAsync(userId)
,而是手动设置安全戳,它将注销用户

public void UpdateSecurityStamp(string userId)
        {
            using (var db = new ApplicationDbContext())
            {
                var user = db.Users.FirstOrDefault(x => x.Id.Equals(userId));
                if (user != null)
                {
                    user.SecurityStamp = Convert.ToString(Guid.NewGuid());
                    db.SaveChanges();
                }
            }
        }

我想根据用户id注销用户。我有管理员权限,可以阻止用户。当用户被阻止时,应该将其注销!也许你需要调用
UserManager.UpdateSecurityStampAsync(userId)在用户下次访问时进行验证时显式执行。为此,您可以实现
ICookieAuthenticationProvider
,并在
OnValidateIdentity
回调上调用它。我想根据用户id注销用户。我有管理员权限,可以阻止用户。当用户被阻止时,应该将其注销!也许你需要调用
UserManager.UpdateSecurityStampAsync(userId)在用户下次访问时进行验证时显式执行。为此,您可以实现
ICookieAuthenticationProvider
,并在
OnValidateIdentity
回调上调用它。您是否找到了此问题的解决方案?我遇到了同样的问题。@AlvinSaldanha,我刚刚发布了对我有效的解决方案。你能找到这个问题的解决方案吗?我遇到了同样的问题。@AlvinSaldanha,我刚刚发布了对我有效的解决方案。感谢Irfan发布解决方案!感谢您的帮助。这与
UserManager.UpdateSecurityStampAsync(user.Id)有何不同。您是否仍然需要删除cookies以强制立即注销?感谢Irfan发布解决方案!感谢您的帮助。这与
UserManager.UpdateSecurityStampAsync(user.Id)有何不同。您是否仍然需要删除cookies以强制立即注销?