c# - 身份 2.0 _LoginPartial 名字

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

我的应用程序是 MVC5 c#,我扩展了 ApplicationUser 模型以包含名字和姓氏,效果很好。我想弄清楚如何更改 loginPartial 以在以下代码中显示用户的实际名字而不是他们的电子邮件地址:

@Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" })

最佳答案

以上是我的问题,很遗憾我无法使用我的旧帐户登录。这是我如何做到的:

在帐户 Controller /登录中,我添加了以下内容:

            var user = await UserManager.FindByNameAsync(model.UserName);
            var t = await UserManager.AddClaimAsync(user.Id, new Claim("FullName", user.FirstName + " " + user.LastName));

添加这个类:

public static class GenericPrincipalExtensions
    {
    public static string FullName (this IPrincipal user)
        {
        if (user.Identity.IsAuthenticated)
            {

            var claimsIdentity = user.Identity as ClaimsIdentity;
            if (claimsIdentity != null)
                {
                foreach (var claim in claimsIdentity.Claims)
                    {
                    if (claim.Type == "FullName")
                        return claim.Value;
                    }
                }
            return "";
            }
        else
            return "";
        }
    }

请参阅上面 Brendan Green 的评论,感谢 Brendan 的领导。 将 _LoginPartial 更改为:

 @Html.ActionLink("Hello " + User.FullName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new {title = "Manage" })

关于c# - 身份 2.0 _LoginPartial 名字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25560428/

相关文章:

c# - AspNet Core Identity,如何设置options.Cookie.SameSite?

C# getdirectories().Length UnauthorizedAccessException

c# - 转换为值类型 'Int32' 失败

javascript - AngularJS: Controller 仅在页面刷新后加载

asp.net - 如何从 ASP.NET Web API 方法内部找到声明的内容?

asp.net - ASP 核心。多重身份的最佳实践

asp.net - 身份和自定义表之间的多对多关系。 EF7-代码优先

c# - 反射发出堆栈和方法调用

c# - SQLCommand 方法 ExecuteScalar() 返回什么数据类型?

c# - 项目构建但无法发布