c# - 用户登录后更新声明

标签 c# asp.net-mvc asp.net-mvc-5 asp.net-identity asp.net-identity-2

是否可以在用户登录后更新声明?我的站点中有前端和管理面板,基本上我使用 claims 来实现此目的。如果用户在前端以 Susan 身份登录,我会在我的代码中执行以下操作:

var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

userIdentity.AddClaim(new Claim("Id", this.UserName));
... //other claims here

所以当用户最终登录到管理面板时(当仍然在前端登录时),我只想添加更多声明,例如:

userIdentity.AddClaim(new Claim("BackEndId", this.UserName));

如何实现?

最佳答案

为了能够读取 cookie 上的声明,您需要在同一请求期间为我们的用户签名并再次让他们重新登录:

idenityt.AddClaim(new Claim("myClaimType", "myClaimValue"));

var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
authenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, idenityt);

关于c# - 用户登录后更新声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31107276/

相关文章:

c# - 查找并用新行替换 ASCII 字符

c# - 如何实现一个通用的c#接口(interface)

asp.net-mvc - xdt :Transform ="Insert" not working for <rewrite> in <system. 网络服务器>

javascript - 添加复选框以允许从我的 html 表中进行多次删除和编辑

c# - 获取 EF TPT 继承中的实体类型

c# - LINQ to Entities无法识别: LastOrDefault [duplicate]方法

c# - 如何对 MVC3 列表中的对象设置的拾取属性进行模型验证?

c# - 写一个二进制文件到struct c#

c# - 为 MVC 应用程序模拟 System.Web.Routing 中的 RouteData 类

javascript - 在 ASP.NET MVC5 中向表单动态添加控件