1

There are many topics on this issue, did not found one to fit my wish.

In MVC5, in _LoginPartial, there is this function User.Identity.GetUserName(), which is displaying the UserName of the actual user.

I want to display the Name, from ApplicationUser, which contain the properties:

public ApplicationUser()
{
   Name = FirstName + " " + LastName;
}
public FirstName {get;set;}
public LastName {get;set;}
public Name {get;}

I have the posibility to use a static function to get the Name, like:

function.Name(User.Identity.GetUserId()).

I do not want to query again the database, I want to use this User.Identity.Name, but this function is returning the UserName, I need to override somehow.

How can I get the Name, using User.Identity.Name?

Lucian Bumb
  • 2,821
  • 5
  • 26
  • 39

2 Answers2

2
User.FindFirst("name").Value

i.e. search through claims to get the display string. "name" = display name for the identity fetched.

Marco
  • 22,856
  • 9
  • 75
  • 124
DKumar
  • 19
  • 3
  • Hi @Dkumar, if you put four spaces in front of your code, it'll be formatted as code which helps clarify the different sections of your answer. – Tom Jun 27 '17 at 09:44
-1

Off the top of my head UserManager has a method to get ApplicationUser from UserId. Then with the ApplicationUser object you can get the Name property. Something like:

ApplicationUser user = UserManager.GetUserById(User.Identity.GetUserId());
user.Name;
GMon
  • 638
  • 6
  • 14