0

when execute this statement SPContext.Current.Web.CurrentUser it gives me user SharePoint\system and when I execute this statement

if (profileManager.UserExists(username))
                        { }

it says user not found.

I logged in with SharePoint\spadmin. why it is returning wrong user name.

6
  • 1
    SharePoint\spadmin in your case may be the app pool account and hence when you login it appears as SharePoint\system account. May 26, 2015 at 6:26
  • but why profileManager.UserExists(username) returns false ? May 26, 2015 at 6:27
  • Perhaps there may not be a profile associated with the system account. Check in the UPS if there is a profile for it. May 26, 2015 at 6:30
  • yes there is account for spadmin but no system account. My question is from where the system account is coming in context and how can I change it to get spadmin account always ? May 26, 2015 at 6:33
  • In SharePoint world, the app pool account is called the system account and hence when you login as spadmin, the context shows the current user as system account May 26, 2015 at 6:36

2 Answers 2

2

SharePoint\spadmin in your case may be the app pool account and hence when you login to SharePoint it appears as SharePoint\system account. In SharePoint world, the app pool account is called the system account. To get the user name of the system account you can use HttpContext.Current.User.Identity.Name Check these for more information:

Get current logged user name for user "Account operates as System"

How to know the exact loginname of the user who operates as system account?

2
  • I fixed it using this statement. is this the correct way ? username = new UserProfileManager(context).GetUserProfile(false).AccountName.ToString(); May 26, 2015 at 6:51
  • @MuhammadZaighum, I am not sure about it. May 26, 2015 at 6:55
1

Since no one else suggested this before, I am leaving this suggestion here in hope it may help somebody.

Like Nadeem already suggested, getting "SharePoint\system" as the current logged user name in a site collection is probably an indication that the current user actually is the same the current web application associated application pool is running as.

For those reason when in doubt in the past I have used the following extension method.

public static string RealLoginName(this SPUser instance)
{
  if (!instance.LoginName.Equals("SHAREPOINT\\system", StringComparison.InvariantCultureIgnoreCase))
     return instance.LoginName;
  else return instance.ParentWeb.Site.WebApplication.ApplicationPool.Username;
}

There may be cases where you don't want to use the SPUser ParentWeb property (or you don't have an SPUser to begin with), but you get the general idea... just get old of the current SPWebApplication context and from here get the application pool running user1. username

1
  • +1, that is nice way of getting the username of system account. May 26, 2015 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.