0

In my ASP.NET Core 1.1.1 app with Identity 3.0, I've extended the default ApplicationUser class as follows. In my following Index view I'm displaying the userName on top bar by using @User.Identity.Name. Question: How can I display the user's full name instead using UserFullName property below?

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

The ASPNETUsers table is already populated with UserName and UserFullName columns and have their values i.e. JSmith, John Smith, BDoe, Bob Doe, etc. So the question is if JSmith is logged in user, we can display his user Hello JSmith as @User.Identity.Name. But how do I display John Smith

nam
  • 21,967
  • 37
  • 158
  • 332

2 Answers2

0

You need to add a calculated field like this.

public string FullName() { return String.Format("{0} {1}",FirstName,LastName); }
  • Probably I was not that clear in the post. I've now added an UPDATE for more details. – nam Oct 20 '17 at 01:45
0

The User in the page is not an ApplicationUser but an IPrinicipal. You need to get the ApplicationUser like this.

 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/