Attention: We are retiring the ASP.NET Community Blogs. Learn more >


"Knowledge has to be improved, challenged, and increased constantly, or it vanishes."

Get the login name of the current user in SharePoint 2013

Recently in one of my project, I wanted to retrieve the logged in user’s login name in the format of domain\username. This seems simple and to retrieve the username, I used the below code.

SPContext.Current.Web.CurrentUser.LoginName

But the output I received contains some extra stuff along with my domain name and username. My domain name is sp1013domain and user name is spsetup, see the output that generated by the above line of code.

i:0#.w|sp2013domain\spsetup

While searching over web, I found some guys speaking about retrieving the domain\username by splitting the above result by pipe (|) symbol, I was not convinced with that. The extra characters added to the login name is by the claims authentication, so definitely we should be able to retrieve it automated. There is a SPClaimProviderManager class that is responsible for management of various claim providers in the SharePoint farm, and there is a DecodeClaim method, that decodes a claim from an encoded string, and for my requirement these are useful to retrieve the required information.

string userName = null;

SPClaimProviderManager mgr = SPClaimProviderManager.Local;

if (mgr != null)

{

userName = mgr.DecodeClaim(SPContext.Current.Web.CurrentUser.LoginName).Value;

}

When I print the userName, I got the below output.

sp2013domain\spsetup

The DecodeClaim method is useful to retrieve the correct login name of the current user.

4 Comments

  • Hi, im new to this stuff. Im just curious on where should this be inputted? Can i input this to the Web Part Script Editor?
    Thanks! :)

  • thanks!
    I wish I knew about it before I've used the string split...

  • Balancing academic tasks can be tough, but the article review writing service proved to be a fantastic support. Their team delivered a thorough and insightful review that demonstrated deep understanding and critical analysis of the material. The service was incredibly efficient, and the paper was tailored to meet my professor's expectations.

  • На этом сайте я смог купить качественные шины размером 195 65 R15 для моего авто. Поэтому я и вам хочу порекомендовать этот сайт, ведь это действительно лучший сайт для покупки шин онлайн что я когда-то использовал. Именно поэтому я теперь буду пользоваться только этим сайтом, если мне надо будет купить новые шины.

Add a Comment

As it will appear on the website

Not displayed

Your website