0

On my _Layout.cshtml page of my site, I just want to display the current user's first and last name. This is stored in the AspNetUsers database table in the Name column.

Anyone know how I can get this data?

I looked in the UserManager class, and I see methods like GetEmail and GetPhone, but nothing to get Name.

Steven
  • 18,761
  • 70
  • 194
  • 296

3 Answers3

1

You can use HttpContext.User property to get this information like

User.Identity.Name

(Or) fully qualified name

HttpContext.Current.User.Identity.Name.

Per your comment, you are trying to get the UserName field from AspNetUsers table.

Take a look at the below posts

Cutting Edge Store User Data in ASP.NET Identity

Using Asp.Net Identity DataBase first approach

Community
  • 1
  • 1
Rahul
  • 76,197
  • 13
  • 71
  • 125
  • unfortunately that only gives me the `UserName` field. I need the data that is stored in the `Name` field in the `AspNetUsers` table – Steven May 30 '15 at 21:08
1

Can you elaborate more where did Name column came from?

Preferred way of adding columns to AspNetUsers table is by enabling migrations in your project, adding a new property to ApplicationUser class and then adding new migrations and applying it.

By doing it that way you will have access to the new Name property when you fetch User from UserManager.

Marko M.
  • 789
  • 3
  • 9
0

Just read your question again and there is no column called Name in the AspNetUsers table, there is a UserName column. First name and last name are not held in the AspNetUsers table, unless you've added them in, in that case I would create a Stored Proc to return the data from that table.

For accessing UserName and UserId,

Add the following using statement.

using Microsoft.AspNet.Identity;

You can now get more methods from the HttpContext.User.Identity.

var UserName = User.Identity.GetUserName();
var UserId = User.Identity.GetUserId();

Thanks

shammelburg
  • 6,974
  • 7
  • 26
  • 34