Get logged in user's id

naveed picture naveed · Dec 21, 2012 · Viewed 34.9k times · Source

How can I get the logged in user's UserId? I'm using the standard system generated AccountModel. I can get the username using:

User.Identity.Name

but I don't see the UserId field. I want to use the UserId as a foreign key for another table.

Answer

prasanthv picture prasanthv · Dec 4, 2013

Try this:

using Microsoft.AspNet.Identity;
User.Identity.GetUserId();

That's how its done in the partial views for current MVC (MVC5/EF6/VS2013) templates.

Correct me if I'm wrong, because I've seen Aviatrix's answers a lot, but what happens if more than one user has the same name in the database?