{"id":6386,"date":"2013-10-25T15:04:18","date_gmt":"2013-10-25T07:04:18","guid":{"rendered":"http:\/\/www.boostsolutions.com\/blog\/?p=6386"},"modified":"2023-07-31T11:38:43","modified_gmt":"2023-07-31T03:38:43","slug":"how-get-sharepoint-user-login-name-in-sharepoint-20102013","status":"publish","type":"post","link":"https:\/\/www.boostsolutions.com\/blog\/how-get-sharepoint-user-login-name-in-sharepoint-20102013\/","title":{"rendered":"How get SharePoint user login name in SharePoint 2010\/2013"},"content":{"rendered":"<p>There are a lot of login authentications in SharePoint, different ways of certification for SharePoint user is different, as well as property.<\/p>\n<p>In SharePoint 2010 you can get user login name by using:<\/p>\n<pre>public static SPPrincipal EnsurePeople(SPWeb web, string peopleName)\r\n        {\r\n            SPPrincipal principal = GetPeople(web, peopleName);\r\n            if (principal != null)\r\n            {\r\n                return principal;\r\n            }\r\n            else\r\n            {\r\n                try\r\n                {\r\n                    SPUser user = null;\r\n                    SecurityUtility.RunWithSystemAccountPrivileges(web, delegate(SPWeb delegateWeb)\r\n                    {\r\n                        delegateWeb.AllowUnsafeUpdates = true;\r\n                        user = delegateWeb.EnsureUser(peopleName);\r\n                        delegateWeb.AllowUnsafeUpdates = false;\r\n                    });\r\n                    return user;\r\n                }\r\n                catch\r\n                {\r\n                    return null;\r\n                }\r\n            }\r\n        }\r\n\t\t public static string GetDecodeUserName(string userName)\r\n        {\r\n            if (!string.IsNullOrEmpty(userName))\r\n            {\r\n                SPPrincipal people = PeopleUtility.EnsurePeople(SPContext.Current.Web, userName);\r\n                if (people != null)\r\n                {\r\n                    string loginName = people.LoginName;\r\n                    if (SPClaimProviderManager.IsEncodedClaim(loginName))\r\n                    {\r\n                        SPClaim claim = SPClaimProviderManager.Local.DecodeClaim(loginName);\r\n                        SPOriginalIssuerType issuerType = SPOriginalIssuers.GetIssuerType(claim.OriginalIssuer);\r\n                        switch (issuerType)\r\n                        {\r\n                            case SPOriginalIssuerType.ClaimProvider:\r\n                            case SPOriginalIssuerType.Forms:\r\n                            case SPOriginalIssuerType.SecurityTokenService:\r\n                            case SPOriginalIssuerType.TrustedProvider:\r\n                            case SPOriginalIssuerType.Unknown:\r\n                                userName = SPClaimProviderManager.Local.DecodeClaim(loginName).Value;\r\n                                break;\r\n                            case SPOriginalIssuerType.Windows:\r\n                                userName = SPClaimProviderManager.Local.ConvertClaimToIdentifier(loginName);\r\n                                break;\r\n                            default:\r\n                                break;\r\n                        }\r\n                    }\r\n                    else\r\n                    {\r\n                        userName = people.LoginName;\r\n                    }\r\n                }\r\n            }\r\n\r\n            return userName;\r\n        }<\/pre>\n<p>In sharepoint 2013 you can get user login name by using:<\/p>\n<pre>SPClaimProviderManager claimManager = SPClaimProviderManager.Local;\r\n\t\tSPClaim userClaim = claimManager.ConvertSPUserToClaim(user);\r\n\t\tstring loginName = claimManager.ConvertClaimToIdentifier(userClaim.ToEncodedString()); \/\/ given a Windows user encoded claim, this method will return a string in the DOMAIN\\USERNAME format.<\/pre>\n<p>Articles for reference:<\/p>\n<p><a href=\"http:\/\/www.wictorwilen.se\/Post\/How-Claims-encoding-works-in-SharePoint-2010.aspx\">How Claims encoding works in SharePoint 2010<\/a> &#8211; Writen by Wictor Wilen, SharePoint MCA, MCSM, MCM and MVP<\/p>\n<p><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.administration.claims.spclaimprovidermanager_methods(v=office.14).aspx\">SPClaimProviderManager Methods<\/a> by Microsoft<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are a lot of login authentications in SharePoint, different ways of certification for SharePoint user is different, as well as property. In SharePoint 2010 you can get user login name by using: public static SPPrincipal EnsurePeople(SPWeb web, string peopleName) { SPPrincipal principal = GetPeople(web, peopleName); if (principal != null) { return principal; } else [&hellip;]<\/p>\n","protected":false},"author":68,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1,461,460],"tags":[],"_links":{"self":[{"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/6386"}],"collection":[{"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/users\/68"}],"replies":[{"embeddable":true,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=6386"}],"version-history":[{"count":8,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/6386\/revisions"}],"predecessor-version":[{"id":6393,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/6386\/revisions\/6393"}],"wp:attachment":[{"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=6386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=6386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=6386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}