Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get current logged in userID in aspnet mvc membership

i am trying to show a list of users of my application "school", when admin logs in then he can view all the users in list but when school principals logins, he should get only users of his school, So i thought to get the current loggedIn userId first and then by that userId i'll get schoolId since userId is foreign key in school table...once i'll get the schoolId i can show the members of that school.

But my problem is how to get the UserID of currently loggedIn. I'm using MVC 1.0 Asp.Net -- "Membership"

if my logic above is wrong then please tell me the alternate good idea, so that principal can see only his users list..

like image 671
FosterZ Avatar asked May 07 '10 16:05

FosterZ


People also ask

How can get current user in ASP NET MVC?

How to get the current logged-in user in an active directory when your ASP.NET MVC application is running on a web server inside the network: // Find currently logged in user UserPrincipal adUser = null; using (HostingEnvironment. Impersonate()) { var userContext = System. Web.

How can check user already login in ASP NET MVC?

You need to set FormsAuthentication. SetAuthCookie(PrimaryKey, false); when user is loggedIn. Here, PrimaryKey is the key that you can use throughout the session for identification of the user using User.Identity.Name . Also, when user log out of the application, you will call FormsAuthentication.

How can I see logged in username in asp net?

You can display the name of the current user using the LoginName control. This control displays the user ID of the current logged-in user, whether the user's identity is established with ASP.NET login controls (and implicitly, ASP.NET membership) or with Integrated Windows authentication.

How can get current user details in asp net core?

You can create a method to get the current user : private Task<ApplicationUser> GetCurrentUserAsync() => _userManager. GetUserAsync(HttpContext. User);


3 Answers

Based on this question and answer by J. Pablo Fernández you can get the user id of the current user with the following code:

//It will only be a Guid if you are using SQL Server as the DB as oppose to MySQL
Guid userGuid = (Guid)Membership.GetUser().ProviderUserKey;

Here is the MSDN Documentation.

like image 138
orandov Avatar answered Oct 17 '22 13:10

orandov


The Simple Solution With the scenario you describe you would only be able to do this by retrieving the User information with the data stored in the HttpContext.Current.Identity. For example, if the HttpContext.Current.Identity.Name is the Username, then you would use that value to retrieve the User data for the principal that should include the UserId you can use to locate the appropriate school.

Alternate Solution You might consider storing the SchoolId in the user's profile so that it is more easily accessible.

like image 20
Roberto Hernandez Avatar answered Oct 17 '22 12:10

Roberto Hernandez


The easiest way that I've tried

You have to include Microsoft.AspNet.Identity

using Microsoft.AspNet.Identity;

Then use it like this

var userId = User.Identity.GetUserId().ToString();
like image 28
Christopher Avatar answered Oct 17 '22 13:10

Christopher