How can I get CURRENT USERNAME in membership asp.net 2008

I use a membership in asp.net 2008. And I build a login system with some rolls …etc

I have a database and tables and the KEY is “username”, and now I want to know how can I get a username for the member who logged in ?

for example:
I logged in as TURKI
I want to get the username TURKI

really I need for help…

thanks,

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

User.Identity.Name

User is a property of the page. If you need to access it elsewhere, you can use:

HttpContext.Current.User.Identity.Name

Method 2

A modification to Freddy’s answer when using MVC – had to use:

HttpContext.User.Identity.Name

Slight change but figured I would post it in case anyone else trying to do this in MVC hits the same snag.

Method 3

Context.User.Identity.Name

Method 4

you can use the Membership.GetUser() method described here:

http://msdn.microsoft.com/en-us/library/system.web.security.membership.getuser.aspx

also, if you do get the MembershipUser you could also get the ‘ProviderUserKey’ which uniquely identifies a user, possibly using that as your FK for your tables, that way your user can have their username updated without having to change all the keys in your tables.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x