我的应用程序是 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/