How to get user profile data from ASP.NET MVC custom IdentityUser Model?

With the help of this blog, I have added a custom profile property OfficeKey to ApplicationUser class (in IdentityModels.cs file):

public class ApplicationUser : IdentityUser
{
      public int OfficeKey { get; set; }

      //remaining code here
}

I can get user name like this:

   User.Identity.Name

How do I get custom user data OfficeKey?

I have tried these:

How to get Identity User Data from Asp.net mvc Model

How to get user email address from ASP.NET MVC Default Mempership Model?

NB: It is an ASP.NET MVC 5 web application with MySQL membership provider.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

If you configure the UserManager with the ApplicationUser it will return the user with the information as follows:

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

// Find user
var user = manager.FindById(User.Identity.GetUserId());

The OfficeKey will be available in the user.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x