How to find the logged in user in Sharepoint?

Brigadier Jigar picture Brigadier Jigar · Mar 11, 2010 · Viewed 65.2k times · Source

I have developed a "web part" that has to be deployed on a Sharepoint server. I need the username of the user, who has logged in the sharepoint server within the web part.

How do I get that username?

Answer

KMån picture KMån · Mar 11, 2010

Following worked for me:

SPWeb theSite = SPControl.GetContextWeb(Context);
SPUser theUser = theSite.CurrentUser;
string strUserName = theUser.LoginName;

and check this out.