Retrieving data assigned to individual users

I am creating an application for the first time using the ASP.NET MVC / Web API, which has several user accounts with "UserId", "AccountId", "ClientId" and "EventId" as identification fields. I can log in the user system, get the UserId, but I can’t figure out how to get the AccountId, ClientId and EventId based on the registered user.

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

I have always looked for help on this issue. I thought it would be fairly straightforward, but I still have to find the answer. Thanks!

+4
source share
1 answer

If you received a standard IdentityUser , you can do it like this:

 // 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 assume that you used the name ApplicationUser ;

So, suppose your class looks like this:

 public class ApplicationUser : IdentityUser { public int AccountId{ get; set; } public int ClientId{ get; set; } // etc } 
+2
source

All Articles