你面对的是
SecurityStampValidator
在行动中。在VS2013/VS2015的标准MVC5模板中,有一个文件
App_Start\Startup.Auth.cs
它有以下几行:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
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.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
你需要看看
SecurityStampValidator.OnValidateIdentity
-此方法每30分钟重新生成一次cookie(在默认配置中)。而且它不保留在
user.GenerateUserIdentityAsync(manager)
.
所以你需要找到
ApplicationUser
类并修改
GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
方法来包括您的索赔。不要在其他地方添加索赔。