Getting the Current Logged in user in SharePoint 2010

We all know the below way to get the current logged in user in SharePoint:

SPWeb web = SPContext.Current.Web;
SPUser user = web.CurrentUser;

but this actually returns the user account running the application pool identity for that site.(if the application pool is configured to run as a standalone/service user account))
Another way to get the current context logged in user is through ensuring the login name:

SPWeb web = SPContext.Current.Web;
string loginName = System.Threading.Thread.CurrentPrincipal.Identity.Name
SPUser user = web.EnsureUser(@"i:" + loginName ); // Had to Append "i:" in my case of Custom Authentication Providers

For more information, please check out SPWeb.EnsureUser

Once we have current SPUser Object, we can also get to his SP Security Groups :

SPUser user = web.EnsureUser(@"i:" + loginName );
SPGroupCollection userGroups = user.Groups;
foreach (SPGroup spgroup in userGroups)
{
}
Advertisements
Posted in Sharepoint 2010
2 comments on “Getting the Current Logged in user in SharePoint 2010
  1. indra says:

    how to counter online user in sharepoint 2010 sir?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: