4

I am building an application for the first time using ASP.NET MVC/Web API that has multiple user accounts with 'UserId', 'AccountId', 'ClientId' and 'EventId' as identifying fields. I am able to login the user, retrieve the UserId, but I cannot figure out how to get the AccountId, ClientId and EventId based on the logged in user.

object id = User.Identity.GetUserId();
ViewData["Id"] = id;

I have looked everywhere for help on this issue. I thought that it would be pretty straight forward but I have yet to find an answer. Thanks!

hutchonoid
  • 32,982
  • 15
  • 99
  • 104
bcedergren
  • 85
  • 1
  • 8

1 Answers1

2

If you have derived the standard IdentityUser you can do it as follows:

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

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

Here I am assuming you have used the name ApplicationUser;

So assuming your class is like this:

public class ApplicationUser : IdentityUser
{
      public int AccountId{ get; set; }
      public int ClientId{ get; set; }
      // etc
}
hutchonoid
  • 32,982
  • 15
  • 99
  • 104