Asp.net MVC丢失时何时重新加载索赔
我在登录时添加了一些自定义声明Asp.net MVC丢失时何时重新加载索赔,asp.net,asp.net-mvc,claims-based-identity,asp.net-identity-2,Asp.net,Asp.net Mvc,Claims Based Identity,Asp.net Identity 2,我在登录时添加了一些自定义声明 private async Task SignInAsync(ApplicationUser user, bool isPersistent) { AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticati
private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
var accesses = _roleAccessRepository.GetAssignedAccess(roleId);
foreach (var access in accesses)
{
identity.AddClaim(new Claim("AccessCode", access));
}
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}
它们工作得很好,直到我最近发现,在某些不活动的情况下,我假设超过30分钟,这些声明就消失了。但是,用户仍在登录
当用户仍然登录时,如何重新加载这些声明
更新:它是如何发生的。
我登录到一个用户,大约30分钟后再次打开并注销。它没有注销,而是刷新了我的页面,没有任何声明。您将面临
SecurityStampValidator
的行动。在VS2013/VS2015的标准MVC5模板中,有一个文件App\u 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))
}
});
app.UseCookieAuthentication(新的CookieAuthenticationOptions
{
AuthenticationType=DefaultAuthenticationTypes.ApplicationOkie,
LoginPath=新路径字符串(“/Account/Login”),
Provider=新CookieAuthenticationProvider
{
//允许应用程序在用户登录时验证安全戳。
//这是一种安全功能,在您更改密码或向帐户添加外部登录时使用。
OnValidateIdentity=SecurityStampValidator.OnValidateIdentity(
validateInterval:TimeSpan.FromMinutes(30),
regenerateIdentity:(管理器,用户)=>user.GenerateUserIdentityAsync(管理器))
}
});
您需要查看SecurityStampValidator.OnValidateIdentity
——此方法每30分钟重新生成一次cookie(默认配置)。并且它不会保留在user.GenerateUserIdentityAsync(manager)
之外添加的声明
因此,您需要找到
ApplicationUser
类并修改GenerateUserIdentityAsync(UserManager管理器)
方法以包含您的声明。不要在其他任何地方添加声明。是否使用状态服务器处理会话状态?我没有处理任何会话状态。确定。在您的服务器上启用Asp.net会话状态服务并配置应用程序以使用它可能值得一看。如果不使用会话状态服务器或数据库来存储会话状态,它可能不会在应用程序处于非活动状态后定期回收后继续存在。我有点困惑,为什么你会丢失声明,而不是登录:/,我会认为这是全部或没有。谢谢你的信息。我不知道这是怎么发生的。我将更新描述。@Coulton这是Asp.Net身份技巧,与会话无关,因为这里根本不使用会话。请注意,谢谢。无论如何,在我这方面的一个小问题是,我在一个单独的类项目上有ApplicationUser
,它被我的所有其他项目使用,因此添加到它会导致循环引用错误。我是否可以在regenerateIdentity
上使用或如何使用SperateGenerateUserIdentity Async
?是的,您可以创建一个新的方法来创建标识,并将其作为SecurityStampValidator
的函数参数传递。您可以为正在使用的每个项目使用其中的一些。我使用了manager
中的新CreateIdentityAsync
。该用户是我已经登录的用户(自索赔丢失后我没有注销),他们的索赔没有被重新加载。请尝试清理cookies并重新登录。出于测试目的,您可以将validateInterval
缩短到几秒钟。