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

I'm creating a web part on a non local server and I need to get the current user logged in to SharePoint. I'm using the following code to get the username.

string currentUser = SPContext.Current.Web.CurrentUser.LoginName;

However, it doesn't return the username of the person logged into SharePoint, instead it returns my username even if someone else is logged in. Does anyone know why this is happening?

I've also tried using the HttpContext and that doesn't work either.

Edit: The answer is kind of embarrassing. I had been storing all the users credentials in a static class, which is why it was always returning the wrong one- it was being set once and never changed. Thank you all for your help.

share|improve this question
    
Can you provide some more details, i.e. are you trying to display the name of the other user through a web part on your server? Are they logged onto server Y, viewing the webpart and you want to display on server X the user viewing the webpart? –  Neil Richards Jul 11 '11 at 19:09
    
yeah for now I'm trying to display the user through a web part. Right now the user goes to the web address where the sharepoint site is and logs in to see the page. The user name that shows up in the upper right is their username, but the one that shows up in the web part is mine. –  Jon Jul 11 '11 at 19:15
1  
@Jon: Instead of editing your question, can you post the solution as an answer below and then accept it? –  Kit Menke Jul 11 '11 at 20:50
add comment

closed as too localized by SPDoctor, Alex Angas Aug 8 '11 at 2:26

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

3 Answers

@Jon's comment moved to answer block

Edit: The answer is kind of embarrassing. I had been storing all the users credentials in a static class, which is why it was always returning the wrong one- it was being set once and never changed. Thank you all for your help.

share|improve this answer
    
Jesus: Voted +1, since this is the answer. But I am also voting to close as too localized, because the behaviour was caused by a specific coding error and not generally applicable. –  SPDoctor Aug 6 '11 at 10:42
add comment

Are you testing the webpart with different user logged in with one browser instance with multiple tabs? Try closing all browser windows, restart the browser and log in with another users' login.

share|improve this answer
    
the user I'm having test it is on a different computer from me so I don't think it's the browser. Would it matter if I was logged on to the server where the web part is deployed? That shouldn't matter because it's logging the user in through the browser right? –  Jon Jul 11 '11 at 19:25
    
Right, that doesn't matter. –  Anita Boerboom Jul 12 '11 at 5:47
add comment

Check in your code if you are using the following delegate:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
  //Code
});

This would be a reason as to why it's happening...

This line of code would return the app pool account.

Check with this line to see if the correct user is being returned:

string user = HttpContext.Current.User.Identity.Name
share|improve this answer
    
I'm not using that delegate anywhere and I've tried HttpContext.Current.User.Identity.Name and that returns the wrong account too –  Jon Jul 11 '11 at 19:42
    
I tried wrapping my code with the delegate and it still doesn't work. –  Jon Jul 11 '11 at 19:53
add comment

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