asp.net-mvc – 更新用户声明不起作用.为什么?
发布时间:2020-05-24 01:14:24 所属栏目:asp.Net 来源:互联网
导读:我正在使用ASP.NET MVC 5.1与Owin和声明身份验证. 用户更改电子邮件后,我需要更新用户声明,所以我在控制器中试过: ClaimsIdentity identity = (ClaimsIdentity)User.Identity; Claim claim = identity.FindFirst(ClaimTypes.Email); identity.RemoveClaim(
|
我正在使用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 – 我如何编写一个ActionFilter来确保AntiForg
- asp.net中的会话,缓存和配置文件有什么区别
- 在ASP.NET 4.5 WebForms中通过bundle.config和BundleConfig
- asp.net核心 – MVC6是否支持预编译视图?
- asp.net – 多租户应用程序的输出缓存,因主机名和文化而异
- 使用ASP.NET MVC时从WebForm访问HtmlHelpers
- asp.net-mvc-3 – 如何有效地从自相关表中加载数据
- 如何将global.asax文件添加到ASP.NET MVC4项目?
- asp.net-mvc – Uploadify(会话和身份验证)与ASP.NET MVC
- asp.net-mvc – 可以将NUnit集成到Visual Studio 2010中吗?
推荐文章
站长推荐
- asp.net-2.0 – 如何解决ASP.NET“”应用程序路径
- asp.net-web-api – 在运行时禁用ApiController
- ASP.NET从内存而不是从文件中流内容
- ASP.NET Web Api HttpResponseException 400(错误
- asp.net-web-api – 使用apicontroller对odata E
- asp.net-mvc – 如何在MVC 4 API中打印JSON脚本
- asp.net – Xamarin.Forms应用程序SQL服务器数据
- asp.net:runat =“server”和服务器控件之间的区
- asp.net中利用Jquery+Ajax+Json实现无刷新分页的
- asp.net – executionTimeout和Server.ScriptTim
热点阅读
