ASP.NET: MemberShip – get the UserID of the User

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..

60 comments ↓

#1 Kjella on 04.21.09 at 9:47 pm

Thanks alot, until now I have retrieved it from the user table, but why not use the inbuild methods 🙂

#2 Marin on 05.29.09 at 2:24 pm

I found it.
Just want to share it with you if somebody needs it:
First have to convert the string to Guid and the to call the method:

Guid UserID = new Guid(“57b0c146-9b75-433b-a6ac-30b598bdbbef”);
Label1.Text= Membership.GetUser(UserID).UserName.ToString();

Good luck

#3 Ian on 12.07.09 at 5:17 am

Thanks heaps!
Fantastic and easy (just the way I like it).

I have to agree with Lulu (Comment 38), ASP.NET forums were less than helpful.
You saved my bacon!
Cheers,

#4 Jimmy Sigenstrøm on 04.20.10 at 4:17 pm

Super nice post.!

And you better believe that this post helps ;o)

#5 phil on 12.03.10 at 11:53 pm

Yep, a real time saver and simple too.

Thanks

#6 G on 05.09.11 at 3:48 am

Beautiful! Posted so long ago and still helped me with ASP.NET 4.0 in MVC3. Thank you!

#7 Sami on 06.15.11 at 9:14 pm

I Love you

#8 nour on 09.10.12 at 7:10 pm

hi guys:

when i need use userid foreign key in other tables in my application set userid datatype string or uniqueidentifier

#9 Josh on 10.19.12 at 4:39 pm

Thanks!

#10 Douglas on 11.12.12 at 9:57 pm

my_aspnet_membership does not contain a definition for GetUser(). Am I missing something? I am using MySQL Connector/NET .

Thanks

Leave a Comment