10

As I can extend methods to access user properties?

There are methods like:

User.Identity.GetUserId()
User.Identity.GetUserName()

Which are accessible from the views and the controllers.

I want to extend this functionality with methods like:

User.Identity.GetUserPhoneNumber()
User.Identity.GetUserLanguaje()
Saghir A. Khatri
  • 3,429
  • 6
  • 45
  • 76
Antonio
  • 103
  • 1
  • 1
  • 4
  • 1
    You could create custom class which inherits from IIdentity or IPrincipal in order to store your new properties. http://stackoverflow.com/questions/1064271/asp-net-mvc-set-custom-iidentity-or-iprincipal – DSR Nov 10 '14 at 09:12

1 Answers1

14

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;
    }
}
Koti Panga
  • 3,660
  • 2
  • 18
  • 21
  • Thanks for your answer. Estoy tratando de poner estos datos en _Layout.cshtml, would be right to do the following , or is it better to use session variables? @{ var manager = new UserManager(new UserStore(new ApplicationDbContext())); var currentUser = manager.FindById(User.Identity.GetUserId()); } – Antonio Nov 10 '14 at 15:17
  • Updated Question see last EDIT section – Koti Panga Nov 10 '14 at 20:33
  • Is there any kind of overhead in retrieving the user? I mean unless it somehow caches it, wouldn't it be going to the DB every time? – thestephenstanton Nov 17 '16 at 15:51
  • Yes there is overhead! Recommendation: Prepare user view-model object with all related props and put it in his session. In specific cases user session object can be refreshed from UserManager/Database. – Koti Panga Nov 17 '16 at 18:59
  • And what about adhering to DRY? Do we REALLY need to write this code in all methods that we want to retrieve user information? – Steve Woods Jan 30 '17 at 16:01
  • Its better to put them in a separate method, and call the method where ever you need. – Koti Panga Feb 08 '17 at 18:25