asp.net – 身份cookie在一段时间后会丢失自定义索赔信息
发布时间:2020-12-15 23:27:59 所属栏目:asp.Net 来源:网络整理
导读:我正在ASP.NET Identity cookie中存储自定义声明,例如用户的真实姓名,以避免在每个请求上进行不必要的数据库查询.至少这是我认为这个代码正在做的: var identity = await user.GenerateUserIdentityAsync(UserManager);identity.AddClaim(new Claim(ClaimTy
我正在ASP.NET Identity cookie中存储自定义声明,例如用户的真实姓名,以避免在每个请求上进行不必要的数据库查询.至少这是我认为这个代码正在做的:
var identity = await user.GenerateUserIdentityAsync(UserManager); identity.AddClaim(new Claim(ClaimTypes.GivenName,user.FirstName))); // etc. AuthenticationManager.SignIn(new AuthenticationProperties {IsPersistent=true},identity); 这工作正常,我可以通过以下方式检索这些声明: private static string GetClaim(string claimType) { var identity = (ClaimsPrincipal) Thread.CurrentPrincipal; var claim = identity.Claims.SingleOrDefault(o => o.Type == claimType); return claim == null ? null : claim.Value; } identity.Claims属性包含以下声明,如预期的: http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier: ced2d16c-cb6c-4af0-ad5a-09df14dc8207 http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name: me@example.com http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider: ASP.NET Identity AspNet.Identity.SecurityStamp: 284c648c-9cc7-4321-b0ce-8a347cd5bcbf http://schemas.microsoft.com/ws/2008/06/identity/claims/role: Admin http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname: My Name 麻烦的是,经过一段时间(通常是几个小时),我的自定义声明似乎消失了 – 在这个例子中,给定名称不再存在于枚举中.用户仍然通过身份验证,所有默认声明仍然存在. 发生了什么,怎么解决这个问题?我唯一可以想到的是,cookie正在过期,并在幕后重新发布,但我不知道为什么(或如果)会发生. 解决方法
是的,这个问题很可能是cookie过期了.由于您没有将自定义声明添加到数据库中的用户声明中,所以它们在刷新时丢失,因为您不会在调用的方法中添加声明.您可以通过以下方式添加声明:
userManager.AddClaim(user.Id,new Claim(ClaimTypes.GivenName,user.FirstName)); 或者您可以在重新生成cookie时调用的方法(默认情况下为user.GenerateUserIdentityAsync)将其移动. 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)) } }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 无法加载文件或程序集’System.Web.WebPage
- asp.net-mvc – 尝试将asp.net web发布到Azure时,Visual St
- Asp.Net路由:如何忽略多个通配符路由?
- asp.net-mvc-3 – MVC3,Ninject和Ninject.MVC3问题
- asp.net – 从GridView中的2列动态生成超链接
- asp.net – 如何在asp中使用会话变量使用c#
- ASP.NET – AJAX / JQUERY的重连接问题
- asp.net – 按值查找TreeView节点
- asp.net-web-api2 – 在控制器操作中获取JSON Web Token有效
- asp.net – 如何向python中的.aspx页面提交查询
推荐文章
站长推荐
- ASP.NET状态管理在适当的情况下
- asp.net-mvc – Ninject MVC和WCF扩展不能与InRe
- asp.net – 如何在MVC 5中为OwinContext设置Time
- .NET的标记SO如何在飞行中呈现?
- asp.net-ajax – 找不到带ID的UpdatePanel
- ASP.NET MVC 微信公共平台开发之获取用户消息并处
- asp.net – Visual Studio IDE的开发
- js_jquery_创建cookie有效期问题_时区问题
- asp.net-mvc – SessionStateTempDataProvider要
- asp.net – 在global.asax中注册并使用Unity的记
热点阅读