Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get logged in user's id

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.

like image 473
naveed Avatar asked Dec 21 '12 17:12

naveed


People also ask

How do you get the User ID of the logged on user in php?

$logged_in_user = mysqli_fetch_assoc($results); $_SESSION['user'] = $logged_in_user; Therefore you should simply be able to get the ID of the logged-in user using the ID column name. Right.

How can we get logged in user details in asp net core?

try this System.Web.HttpContext.Current.User.Identity.Name ? It should work. See the samples in asp.net github.com/aspnet/Identity/blob/…. Just make sure user is logged in.


2 Answers

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?

like image 152
prasanthv Avatar answered Oct 01 '22 23:10

prasanthv


I think you're looking for ProviderUserKey - Gets the user identifier from the membership data source for the user.

object id = Membership.GetUser().ProviderUserKey

Membership.GetUser() - Gets the information from the data source and updates the last-activity date/time stamp for the current logged-on membership user.

like image 33
Erik Philips Avatar answered Oct 02 '22 00:10

Erik Philips