10

I have added First and Last name to the ApplicationUser Class.

 public class ApplicationUser : IdentityUser
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }

        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

also added information to RegisterViewModel

My application successfully created First and LastName to the table but I am unable to get the first and last names as to display in _LoginPartial "User.Identity.Name"

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Emil
  • 281
  • 1
  • 2
  • 11

2 Answers2

13

in your partialview _LoginPartial.cshtml

Add the code:

@if (Request.IsAuthenticated)
{
    var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
    var user = manager.FindById(User.Identity.GetUserId());
...
}

ApplicationDbContext = your DBContext -> Default : ApplicationDbContext

With the instance of ApplicationUser (user) you can get First- and LastName-property

Replace User.Identity.Name with user.FirstName + " " + user.LastName like this:

<ul class="nav navbar-nav navbar-right">
    <li>
        @Html.ActionLink("Hello " + user.FirstName + " " + user.LastName + "!", "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" })
    </li>
    <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
</ul>
Edi G.
  • 2,432
  • 7
  • 24
  • 33
  • yes that is , but the "User.Identity" doesn't seems to have the information – Emil Sep 03 '14 at 13:12
  • Did you add the properties Firstname and Lastname in the AccountViewModels.cs ? – Edi G. Sep 03 '14 at 13:18
  • this is added to "public class RegisterViewModel" and this created the data in the table – Emil Sep 03 '14 at 13:30
  • -1. This would not work. `User.Identity` is of type `IIdentity` and it does not have method `GetName()` – trailmax Sep 03 '14 at 16:36
  • trailmax: right.. i coudn't test my answer.. i have corrected my answer – Edi G. Sep 03 '14 at 20:00
  • 2
    @EdiG. Yes, this will work. But beware, on every request this will cause extra calls to the database which can be avoided. And if we are talking 1M users in the table, this extra call will bring the application to the halt. – trailmax Sep 03 '14 at 21:27
  • 3
    This works well, of note is that I had to add some using statements for it to know what all this stuff was: using MyApp.Models using Microsoft.AspNet.Identity.EntityFramework – CuddleBunny Mar 23 '15 at 17:07
3

You'll need to add names as a Claim on user when user is logged in. Then in your views access these claims from ClaimsPrincipal.Current.Claims. Have a look on my answer here that does pretty much the same, only you need to add names instead of theme.

Community
  • 1
  • 1
trailmax
  • 34,305
  • 22
  • 140
  • 234