ASP.NET标识未“保存”更新的声明

maxshuty

长话短说,我使用身份,在我的解决方案中,我创建了一个自定义帐户设置页面,该页面运行良好且美观。问题是我有用户FirstName并且LastName_Layout.cshtml名称是由我具有的自定义帮助程序方法设置的:

public static MvcHtmlString GetUsersFirstAndLastName(this HtmlHelper helper)
{
   string fullName = HttpContext.Current?.User?.Identity?.Name ?? string.Empty;

   var userIdentity = (ClaimsPrincipal)Thread.CurrentPrincipal;
   var nameClaim = identity?.FindFirst("fullname");

   if (nameClaim != null)
   {
       fullName = nameClaim.Value;
   }

   return MvcHtmlString.Create(fullName);
}

在用户转到其个人资料并更新其名称之前,此方法非常有效。如果他们将名称从更改为GeorgeBob那么当他们在我的网站上浏览时,此方法仍会提取其名称,George直到他们注销并重新登录。

因此,我要解决的问题是当他们在帐户设置中更新其名称时,我添加了一些代码来删除其旧的fullName声明,并添加新的声明,如下所示:

var identity = User.Identity as ClaimsIdentity;

// check for existing claim and remove it
var currentClaim = identity.FindFirst("fullName");
if (currentClaim != null)
   identity.RemoveClaim(existingClaim);

// add new claim
var fullName = user.FirstName + " " + user.LastName;

identity.AddClaim(new Claim("fullName", fullName));

通过这段代码,_Layout视图现在更新了名称(在我们之前的示例中,George现在更改为Bob)。但是,从该视图中单击该按钮到网站上的其他地方或刷新页面的那一刻,该更改会立即变为George

仍然对身份有点陌生,我有些困惑,为什么这个新的更新的声明在他们单击到另一个页面或刷新后不起作用。任何帮助表示赞赏。:)

maxshuty

添加新声明时,您还需要执行以下操作:

    var authenticationManager = HttpContext.GetOwinContext().Authentication;
    authenticationManager.AuthenticationResponseGrant = new AuthenticationResponseGrant(new ClaimsPrincipal(identity), new AuthenticationProperties() { IsPersistent = true });

因此,新的完整代码块为:

public static MvcHtmlString GetUsersFirstAndLastName(this HtmlHelper helper)
{
    string fullName = HttpContext.Current?.User?.Identity?.Name ?? string.Empty;

    var userIdentity = (ClaimsPrincipal)Thread.CurrentPrincipal;
    var nameClaim = identity?.FindFirst("fullname");

    var authenticationManager = HttpContext.GetOwinContext().Authentication;
    authenticationManager.AuthenticationResponseGrant = new AuthenticationResponseGrant(new ClaimsPrincipal(identity), new AuthenticationProperties() { IsPersistent = true });

    if (nameClaim != null)
    {
        fullName = nameClaim.Value;
    }

    return MvcHtmlString.Create(fullName);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

ASP.NET身份未“保存”更新的声明

来自分类Dev

更新 ASP.NET 标识

来自分类Dev

在asp.net的gridview中行未更新

来自分类Dev

CheckBoxList未更新-ASP.Net

来自分类Dev

ASP.NET MVC 视图未更新

来自分类Dev

具有ASP.NET标识的MVC 5-用户登录时获取声明

来自分类Dev

ListView控件中的ASP.NET更新面板未更新

来自分类Dev

发布时未保存ASP.NET MVC日期

来自分类Dev

回发后,Asp.net控件未更新

来自分类Dev

Asp.Net Datagrid视图在新插入时未更新

来自分类Dev

在ASP.NET MVC中转换配置未更新

来自分类Dev

文本框值在asp.net中未更新

来自分类Dev

Mysql 更新查询未运行 ASP.NET

来自分类Dev

架构和 ASP.Net 标识

来自分类Dev

如何获取asp.net MVC中最近保存(最近未保存)行的ID?

来自分类Dev

ASP.NET中的更新导致异常:“必须声明标量变量”

来自分类Dev

ASP.NET CORE 中的自定义身份验证和更新声明

来自分类Dev

从Table ASP.NET更新

来自分类Dev

更新重定向ASP .net

来自分类Dev

ASP.NET身份“基于角色”的声明

来自分类Dev

ASP.NET身份和声明

来自分类Dev

ASP.NET MVC 5 获取声明

来自分类Dev

如何在ASP.NET Core应用程序中显示和更新自定义标识字段?

来自分类Dev

子类未保存在ASP.Net MVC身份模型中的数据库中

来自分类Dev

ASP Repeater生成的Javascript数组未更新

来自分类Dev

从asp.net核心标识获取applicationuser的属性

来自分类Dev

在表中添加具有ASP.NET标识的列

来自分类Dev

MVC 5和ASP.NET标识-实现混乱

来自分类Dev

在ASP.NET中标识用户所属的租户