如何在ASP.NET身份显示用户配置文件属性
问题描述:
在我的ASP.NET Core 1.1.1
应用程序与Identity 3.0
,我已经扩展默认ApplicationUser
类如下。在我的以下Index
视图中,我使用@User.Identity.Name
在顶部栏上显示userName
。 问题:如何显示用户的全名,而不是使用下面的UserFullName
属性?如何在ASP.NET身份显示用户配置文件属性
public class ApplicationUser : IdentityUser
{
public string UserName { get; set; }
[Column(TypeName = "varchar(45)")]
public string UserFullName { get; set; }
[Column(TypeName = "varchar(15)")]
}
Index.cshtml
@using Microsoft.AspNetCore.Identity
@inject SignInManager<ApplicationUser> SignInManager
@if (SignInManager.IsSignedIn(User))
{
Hello @User.Identity.Name
...
}
UPDATE
的ASPNETUsers
表已填充了UserName
和UserFullName
列,并有自己的价值观,即JSmith, John Smith
,BDoe, Bob Doe
,等于是问题是如果登录用户JSmith
,我们可以显示y他的用户Hello JSmith为@User.Identity.Name
。但是,如何显示John Smith
答
您需要添加一个像这样的计算字段。
public string FullName() { return String.Format("{0} {1}",FirstName,LastName); }
答
页面中的用户不是ApplicationUser,而是IPrinicipal。你需要像这样得到ApplicationUser。
ApplicationUserManager manager=
HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
var currentUser = manager.FindById(User.Identity.GetUserId());
// use currentUser.UserFullName
也许我是不是在后说清楚。我现在添加了更多的细节更新。 – nam