I’ve read that question on many message boards so I decided to post a quick blog entry about that. It’s actually very easy.
If you use the new comfortable Login Controls of ASP.NET v2, here is how you resolve the UserID of the currently logged in user:
MembershipUser myObject = Membership.GetUser();
string UserID = myObject.ProviderUserKey.ToString();
That’s all! Hope that helps..
44 comments ↓
Oh yea, thanks. Google is your friend
I asked on asp.net for those few lines but didn’t get an answer, until now, thanks!
If I have this code inside a class, should I use a httpcontext ?
I am not sure when I have to use HttpContext.Current
In this case you don’t have to use HttpContext, Membership is globally available.
Thank u ,Very much !
Spent a long time looking for this else where. Your a star!
thanks it very usfull could you please give some code sample i tried to get the userid i could not
many thanks
Man that saved a headache! Thank you!!!
Thank you guy!!
As simple as that. Thank you so much.
When I try to use this I get:
Membership does not contain a definition for GetUser(). Am I missing something?
Thanks.
Cheers mate! After 2 hours of google, finally a straight forward answer!
Nice one! Many thanks~
Translation to VB.NET
Dim UserID As String
Dim MemUser As MembershipUser
MemUser = Membership.GetUser()
UserID = MemUser.ProviderUserKey.ToString()
Helpful tip out there
But how to get the userid when creating a user using createuserwizard of asp.net 2.0
Hi shazi, you can use this here:
29 MembershipUser user = Membership.GetUser((sender as CreateUserWizard).UserName);
30 string UserID = user.ProviderUserKey.ToString();
hth,
Andreas
I love you - thanks.
I am definately bookmarking your blog.
Yesssssss.. I was always think there is some way microsoft developers implemented to get user GUID.. Thanx alot
Simplifying just a little..
Dim UserID as String
UserID = Membership.GetUser.ProviderUserKey.ToString()
And Thanks! I had been looking for an easy way to get the UserID for a long time!
Hi
I get the following error when trying to get the UserID of the table [aspnet_Users].
Error: Conversion failed when converting from a character string to uniqueidentifier
Code used:
MembershipUser _user = Membership.GetUser();
DataFile.InsertParameters["commited_to_database_by_UserId"].Type = TypeCode.Empty;
DataFile.InsertParameters["commited_to_database_by_UserId"].DefaultValue = _user.ProviderUserKey.ToString();
I am using active directory as my membership provider, should this make any diffrence to the code required.
Hi Trevor,
ProviderUserKey is returning an object. It looks like you need a UniqueIdentifier for your code instead of a string. Try to cast it as Guid.
hth
I’m trying to use this function Membership.GetUser() from LoggedIn event of Login Control in ASP.NET 2/C#. but it is returning null? Any clue
trialblazer,
you’d have to use something like:
MembershipUser myuser = Membership.GetUser(UserName)
hth,
Andreas
Thanks, that’s what I have been looking for.
Simple! Thanks a ton!
I’ve been looking for this for a while now - thanks!
Thanks a lot
Thanks a lot,
This is very helpfull for me,
May Allah success you….
Thank you this helps.
Ok, but how would you put into an insert parameter…so when someone adds a record I know what user added it.
Do you do in a hidden field or in the asp:Parameter? And please show/write the example verbatum since I am an new asp.net user.
I tried this but didn’t work in C# 3.5
<input id=”UserID” type=”hidden” value=”" />
Thx for this hint
Hello,
This piece of code is great! I
just have one HUGE problem: when I try to get the ProviderUserKey the server always returns the same key!!!
I use
“MembershipUser user = Membership.GetUser(username);”
and then
“user.ProviderUserKey.ToString();”
and the ProviderUserKey is always the same whatever user logs in… I don’t really understand why!
Any suggestions?
Thanks in advance!
I think this code works only for the currently logged in users? I mean username should be passed before getting UserID from database.
What if for some email verification purpose UserID is required before login process.
Pedro:
Check your Application ID settings, I think you’re running 2 different Application IDs in your Membership Environment.
Nikks:
In that case you just pass on the UserID or UserName to Membership.GetUser();.
For example:
MembershipUser myObject = Membership.GetUser(”Bill”);
I have trouble when i try to register a new user while logged in with another user using the CreateUserWizard-control and then login with the new account.
Then I get
Membership.GetUser().UserName == null
Other then that UserName returns the correct login name.
Any ideas?
Thank you very much, been searching for hours for this!
ASP.NET Forums are useless, i’ve never got a reply in shorter than a few weeks of waiting, and even then they’ve never solved my issues.
Pretty useless when you’be got to meet deadlines.
Thanks for this!
You can always do it as a 1 liner…
string userId = Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString();
Thank you so much, and to Scott for the VB version. And thanks to Google for finding this solution so quickly
I am getting this error
MembershipUser user = Membership.GetUser((sender as CreateUserWizard).UserName);
string UserID = user.ProviderUserKey.ToString();
Response.Write(UserID);
Object reference not set to an instance of an object.
Any Idar Please Help..
For Ddon.
Maybe:
Membership.GetUser() is definitely null in your case. Meaning that the user
has not actually been logged in
Thanks!
Membership.GetUser() gets the currently logged in user. If you are trying to GetUser() during log in it will always be null.
You have to do GetUser() AFTER the user is logged in, which means AFTER the page lifecycle of logging in.
I have run into this same problem. What I am doing now is using GetUser(UserName) which doesn’t depend on the user being authenticated.
Leave a Comment