2

Using standard windows authentication I attempting to get the identity of the logged in user so I can update a value in a list that this webpart is interacting with.

I have tried:

SPWeb currentWeb = SPContext.Current.Web;
SPUser currentUser = currentWeb.CurrentUser;
return currentUser.ID;

Which only returns the SHAREPOINT\system account.

I have also tried this:

SPWeb web = SPContext.Current.Web;
string loginName = System.Threading.Thread.CurrentPrincipal.Identity.Name
SPUser user = web.EnsureUser(loginName); 

the second line will always come up as an empty string.

5
  • What happens when you login with another user and then run SPContext.Current.Web.CurrentUser? Jul 9, 2012 at 20:46
  • That is the weird thing, is I am logging in using a local windows account. Jul 9, 2012 at 20:49
  • 1
    Was this windows account the same one with which SharePoint was installed? In that case, you could try to create a new user in AD and then login using the new user credentials. Jul 9, 2012 at 20:51
  • @VardhamanDeshpande that was absolutely what was going on, I was using the same account as the one that SharePoint was install. Creating a dummy account and testing with it yielded the expected results using the SPContext.Current.Web.CurrentUser. If you created an answer, I would mark it as correct. Jul 10, 2012 at 12:34
  • Created an answer. Glad I could help. Jul 10, 2012 at 15:05

3 Answers 3

3

Was this windows account the same one with which SharePoint was installed? In that case, you could try to create a new user in AD and then login using the new user credentials.

1
  • To avoid such issues, I always create dedicated service account, even on dev box. This will avoid some issues like this one, or the fact that workflows won't start automatically with system account.
    – Steve B
    Jul 10, 2012 at 15:26
0

The first block of code should work properly - unless you are in a block that is running with elevated privileges. If that is the case, you will need to get the current user ID prior to the ElevatedPrivs block.

2
  • 1
    Since it is a sandbox environment, running with elevated privileges is not possible. But I agree that the first block should run correctly. Jul 9, 2012 at 20:47
  • 2
    True. the only other option would be if the user account he is using to view the site happens to be the system account...
    – Dave Wise
    Jul 9, 2012 at 20:53
0

To get the current user name you have to use SPContext.Current.Web.CurrentUser.Name; now this would return System Account or SPContext.Current.Web.CurrentUser.LoginName this would return SHAREPOINT\system account when you are logged in as the SharePoint administrator(i.e. the account from which you installed the SharePoint). If you want not to get this username then i'm afraid you have to create another account in the AD in order to fix this.

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.