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

If current logged user operates as "System Account", SPContext.Current.Web.CurrentUser.LoginName will return "sharepoint\system", so I want to know the exact loginname of the user. Here is what I tried:

SPUser user = SPContext.Current.Web.CurrentUser;
if (user.LoginName.ToLower().Equals(@"sharepoint\system"))
{
    string loginName = HttpContext.Current.User.Identity.Name;
    // do something...
}  

It works fine at the begining, but I met a problem when I test it in "Claims-Based Authentication" webapplication, I found:

SPContext.Current.Web.CurrentUser.LoginName =  sharepoint\system (The real loginname is i:0#.w|domain\user); 
HttpContext.Current.User.Identity.Name = 0#.w|domain\user;  

So, Why does HttpContext.Current.User.Identity.Name return "0#.w|domain\user" instead of "i:0#.w|domain\user"? How to get current user's loginname correctly? (I want to get "i:0#.w|domain\user")
Thanks in advance!

share|improve this question
    
Well, non of your end users should really be logged in as system account, so I fail to see the problem. What is the objective of this? –  Robert Lindgren May 30 '13 at 9:33
    
@RobertLindgren Thanks for your information. It comes from a bug in our testing, we created an audience, and its rule is "Account Name = domain/currentuser", so if current user operates as system account, AudienceManager.IsMemberOfAudience will returns false, but it should return true actually. So, that's why I asked the question. –  Xiaodan Mao May 30 '13 at 11:55
    
I recognize the situation :P I usally just hard code that "if system user: do this and that" –  Robert Lindgren May 30 '13 at 11:57
add comment

2 Answers 2

Try this:

string userName = String.Empty;
SPClaimProviderManager mgr = SPClaimProviderManager.Local;
if(mgr != null)
{
   userName = mgr.DecodeClaim(SPContext.Current.Web.CurrentUser.LoginName).Value;
}
share|improve this answer
1  
Thanks for your answer, but if current user operates as system account, the loginname is "sharepoint\system", your code will throw exception. –  Xiaodan Mao May 30 '13 at 23:56
add comment

This may be solve your purpose

http://sharepoint2010insight.blogspot.in/2010/11/sharepoint-2010-get-claim-username-and.html

http://practicalsharepoint2010.blogspot.in/2012/02/logged-in-username-sharepoint-2010.html

share|improve this answer
1  
While this may theoretically answer the question, we prefer inclusion of the essential parts of the answer here, and to provide the link for reference. See answer for general guidelines. –  SPDoctor May 30 '13 at 14:33
    
Thanks, but it needs a correct loginname, in my situation, HttpContext.Current.User.Identity.Name returns "0#.w|domain\user", which should be "i:0#.w|domain\user" actually. Do you know why? And SPContext.Current.Web.CurrentUser.LoginName returns "sharepoint\system", which can not be decoded. –  Xiaodan Mao May 31 '13 at 1:30
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.