asp.net-mvc – 更新用户声明不起作用.为什么?
发布时间:2020-12-15 23:39:33 所属栏目:asp.Net 来源:网络整理
导读:我正在使用ASP.NET MVC 5.1与Owin和声明身份验证. 用户更改电子邮件后,我需要更新用户声明,所以我在控制器中试过: ClaimsIdentity identity = (ClaimsIdentity)User.Identity; Claim claim = identity.FindFirst(ClaimTypes.Email); identity.RemoveClaim(c
我正在使用ASP.NET MVC 5.1与Owin和声明身份验证.
用户更改电子邮件后,我需要更新用户声明,所以我在控制器中试过: ClaimsIdentity identity = (ClaimsIdentity)User.Identity; Claim claim = identity.FindFirst(ClaimTypes.Email); identity.RemoveClaim(claim); identity.AddClaim(new Claim(ClaimTypes.Email,newEmail)); IOwinContext context = new OwinContext(); context.Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie); context.Authentication.SignIn(identity); 声明已更改,但是当我刷新页面时,电子邮件声明是原来的… 看来cookie没有被更新.任何想法我做错了什么? 并且可以从身份获取“IsPersistent”的值,所以当我再次签名时,我将具有相同的值? 谢谢, 米格尔 解决方法
我有同样的问题,所以只是想在这里总结我的发现.正如克里斯所说,答案的依据确实在这里:
How to change authentication cookies after changing UserName of current user with asp.net identity,但是我发现线程有点难以跟踪,而且这个问题并不是一个直接的重复.
开始,从当前OWIN上下文获得AuthenticationManager.一旦你这样做,你可以通过调用AuthenticateAsync方法来获取“isPersistent”(和原来的SignIn调用的其他属性)的值.然后更新当前用户身份的声明,您只需要替换AuthenticationResponseGrant属性的值,如下所示: var identity = (ClaimsIdentity)User.Identity; // Call AddClaim,AddClaims or RemoveClaim on the user identity. IOwinContext context = Request.GetOwinContext(); var authenticationContext = await context.Authentication.AuthenticateAsync(DefaultAuthenticationTypes.ExternalCookie); if (authenticationContext != null) { authenticationManager.AuthenticationResponseGrant = new AuthenticationResponseGrant( identity,authenticationContext.Properties); } 它是实际更新cookie的AuthenticationResponseGrant属性的最终设置. 希望这有助于其他读者. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- asp.net-mvc – MVC模型绑定:为什么我不能绑定到
- 如何将TextBox双向绑定到ASP.NET中的代码隐藏属性
- asp.net – 如何Html.Encode在Webforms
- 我可以使用ASP.NET获得“WAR文件”类型部署吗?
- asp.net – “viewstate MAC的验证失败.如果这个
- asp.net-mvc-4 – 如何永久教Visual Studio检测我
- asp.net-mvc – 如何在视图中获取会话值asp.net核
- 你真的了解ASP.NET Core 部署模型吗?
- asp.net – 仅在本地IIS服务器上支持创建虚拟目录
- 简单好用的ASP.NET分页类(支持AJAX、自定义文字
热点阅读