Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# User Authentication with ASP.NET Identity Adding User Sign-In and Sign-Out Refactoring the Menu Into a Partial View

Mark Ramos
Mark Ramos
19,209 Points

Displaying custom profile field for First Name instead of Email Address

I wanted to try displaying the logged-in user's First Name instead of their email address as suggested in the tutorial, and read through the link (https://msdn.microsoft.com/en-us/magazine/dn818488.aspx) provided. I eventually got it to work, but I feel like I didn't quite understand what I did and am wondering if I did it the most correct way. I get lost on the article at the link above when he starts to get into RavenDB.

User.cs

using Microsoft.AspNet.Identity.EntityFramework;

namespace Treehouse.FitnessFrog.Shared.Models
{
    public class User : IdentityUser
    {
        // added fields
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

AccountController.cs Register POST method

[HttpPost]
public async Task<ActionResult> Register(AccountRegisterViewModel viewModel)
{
            // code here removed for brevity, same as in course
            // Instantiate a User object
            var user = new User { UserName = viewModel.Email, Email = viewModel.Email, FirstName = viewModel.FirstName, LastName = viewModel.LastName };

            // code here removed for brevity, same as in course

    return View(viewModel);
}

_Menu.cshtml

<div class="navbar-right">
  @if (Request.IsAuthenticated)
  {
     // code here removed for brevity, same as in course
    // new code start
    var manager = new ApplicationUserManager(new UserStore<User>(new Context()));
    var currentUser = manager.FindById(User.Identity.GetUserId());
    <h5 class="navbar-text" style="margin-top:20px; ">
      Hey @currentUser.FirstName!
    </h5>
   // new code end
  // code here removed for brevity, same as in course
</div>

The changes above show the first name in the header, but was this the best way to go about it?