如何在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表已填充了UserNameUserFullName列,并有自己的价值观,即JSmith, John SmithBDoe, Bob Doe,等于是问题是如果登录用户JSmith,我们可以显示y他的用户Hello JSmith为@User.Identity.Name。但是,如何显示John Smith

您需要添加一个像这样的计算字段。

public string FullName() { return String.Format("{0} {1}",FirstName,LastName); } 
+0

也许我是不是在后说清楚。我现在添加了更多的细节更新。 – nam

页面中的用户不是ApplicationUser,而是IPrinicipal。你需要像这样得到ApplicationUser。

ApplicationUserManager manager= 
    HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); 

var currentUser = manager.FindById(User.Identity.GetUserId()); 

// use currentUser.UserFullName 

https://blogs.msdn.microsoft.com/webdev/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates/