Get logged in user's id
Asked Answered
A

4

18

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.

Anastaciaanastas answered 21/12, 2012 at 17:57 Comment(1)
Simple Membership? ASP.NET Membership?Eighteenth
B
15

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.

Bordure answered 21/12, 2012 at 18:9 Comment(3)
Hmm, I'm getting: Error 1 'System.Web.Security.MembershipUser' does not contain a definition for 'GetUser'Anastaciaanastas
Thanks, typo there, it was suppose to be Membership.Bordure
Membership.GetUser().UserName for the username instead of the internal membership database keyCharissecharita
B
26

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?

Bolding answered 4/12, 2013 at 3:9 Comment(1)
This solution is not possible if the user is already logged in and trying to change the current password.Rojo
B
15

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.

Bordure answered 21/12, 2012 at 18:9 Comment(3)
Hmm, I'm getting: Error 1 'System.Web.Security.MembershipUser' does not contain a definition for 'GetUser'Anastaciaanastas
Thanks, typo there, it was suppose to be Membership.Bordure
Membership.GetUser().UserName for the username instead of the internal membership database keyCharissecharita
G
10

The best way to do so is to use the WebSecurty class

var memberId = WebSecurity.GetUserId(User.Identity.Name);

and don't forget to add [InitializeSimpleMembership] on top of your controller :)

Gelasius answered 21/12, 2012 at 21:11 Comment(1)
As long as your provider follows the requirements to use an int as the Unique Identifier.Bordure
S
0

Their are already very good answer but what i understood from your question is that you want to fetch data from database using id.so here is what you can do.

public List<ForeignkeyTable> RV()
{
 var email = User.Identity.Name;

Usertable m = db.Uusertables.Where(x => x.user_Email == email).SingleOrDefault();

 var id = m.user_Id;

var p = db.ForeignkeyTable.Where(x => x.user_fk_id == id).ToList();


return p;

}

This way you can return all the data from database of that id in foreignkey.

Soffit answered 19/7, 2019 at 8:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.