Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
    
What happens when you login with another user and then run SPContext.Current.Web.CurrentUser? –  Vardhaman Deshpande Jul 9 '12 at 20:46
    
That is the weird thing, is I am logging in using a local windows account. –  aceinthehole Jul 9 '12 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. –  Vardhaman Deshpande Jul 9 '12 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. –  aceinthehole Jul 10 '12 at 12:34
    
Created an answer. Glad I could help. –  Vardhaman Deshpande Jul 10 '12 at 15:05
add comment

3 Answers 3

up vote 3 down vote accepted

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.

share|improve this answer
    
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 '12 at 15:26
add comment

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.

share|improve this answer
1  
Since it is a sandbox environment, running with elevated privileges is not possible. But I agree that the first block should run correctly. –  Vardhaman Deshpande Jul 9 '12 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 '12 at 20:53
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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