OnValidateIdentity ASP.NET Identity是如何工作的?

7

我希望能更好地理解.NET的Identity OnValidateIdentity方法是如何工作的。我已经在我的应用程序中设置了以下代码:

app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/Account/Login"),
        CookieName = "LoginCookie",
        ExpireTimeSpan = TimeSpan.FromHours(1),
        Provider = new CookieAuthenticationProvider
        {
            // Enables the application to validate the security stamp when the user logs in.
            // This is a security feature which is used when you change a password or add an external login to your account.  
            OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                validateInterval: TimeSpan.FromHours(1),
                regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
        }
    });

这里的OnValidateIdentity有一个检查角色,用于检查用户访问我的网站时cookie的年龄是否比我在此处设置的cookie的年龄(即1小时)更老 - 如果是,则强制用户重新登录应用程序。这就是它确切的工作方式吗?
1个回答

9
如果你想更全面地了解,为什么不阅读源代码呢?
简而言之,此方法将检查用户记录上的SecurityStamp值是否已更改。它将在每小时(在您的设置中)进行检查。因此,如果SecurityStamp已更改,则cookie将无效。如果SecurityStamp与上次检查时相同,则更新cookie的值(带有新时间戳),但用户不会注销。
当用户更改密码并希望使所有现有的浏览器中的auth-cookie无效时,此功能非常有用。
在我的博客文章中有更详细的说明。

1
这并不是整个故事,因为他正在调用 Microsoft.AspNet.Identity.Owin.SecurityStampValidator - JHBonarius

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接