C# – ASP.NET Identity Extend methods to access user properties


As I can extend methods to access user properties?

There are methods like:


Which are accessible from the views and the controllers.

I want to extend this functionality with methods like:


Best Solution

Similar Question: Need access more user properties in User.Indentity answered by Microsoft professionals at codeplex worklog as below

"You can get the User object and do User.Email or User.PhoneNumber since these properties are hanging off the User model"

We can get the application current User object in ASP.Net identity as below, from there you can access all properties of user object, we follow the same in mvc5 web-apps as of now.

//In Account controller like this
var currentUser = UserManager.FindById(User.Identity.GetUserId());

In other controllers you will need to add the following to your controller:

  var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));

  // Get the current logged in User and look up the user in ASP.NET Identity
  var currentUser = manager.FindById(User.Identity.GetUserId()); 

Now we can access all user props(Phone# and Language) as below

var phoneNumber = currentUser.PhoneNumber;
var userLanguage = currentUser.Language;

EDIT: If you want to retrieve the same in any view.cshtml or _Layout.cshtml then you should do like below

@using Microsoft.AspNet.Identity
@using Microsoft.AspNet.Identity.EntityFramework
@using YourWebApplication.Models

    var phoneNumber= string.Empty;
    var userLanguage = string.Empty;
    if (User.Identity.IsAuthenticated) {
        var userStore = new UserStore<ApplicationUser>(new ApplicationDbContext());
        var manager = new UserManager<ApplicationUser>(userStore);
        var currentUser = manager.FindById(User.Identity.GetUserId());

        phoneNumber = currentUser.PhoneNumber;
        userLanguage = currentUser.Language;