How get SharePoint user login name in SharePoint 2010/2013

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
            {
                try
                {
                    SPUser user = null;
                    SecurityUtility.RunWithSystemAccountPrivileges(web, delegate(SPWeb delegateWeb)
                    {
                        delegateWeb.AllowUnsafeUpdates = true;
                        user = delegateWeb.EnsureUser(peopleName);
                        delegateWeb.AllowUnsafeUpdates = false;
                    });
                    return user;
                }
                catch
                {
                    return null;
                }
            }
        }
		 public static string GetDecodeUserName(string userName)
        {
            if (!string.IsNullOrEmpty(userName))
            {
                SPPrincipal people = PeopleUtility.EnsurePeople(SPContext.Current.Web, userName);
                if (people != null)
                {
                    string loginName = people.LoginName;
                    if (SPClaimProviderManager.IsEncodedClaim(loginName))
                    {
                        SPClaim claim = SPClaimProviderManager.Local.DecodeClaim(loginName);
                        SPOriginalIssuerType issuerType = SPOriginalIssuers.GetIssuerType(claim.OriginalIssuer);
                        switch (issuerType)
                        {
                            case SPOriginalIssuerType.ClaimProvider:
                            case SPOriginalIssuerType.Forms:
                            case SPOriginalIssuerType.SecurityTokenService:
                            case SPOriginalIssuerType.TrustedProvider:
                            case SPOriginalIssuerType.Unknown:
                                userName = SPClaimProviderManager.Local.DecodeClaim(loginName).Value;
                                break;
                            case SPOriginalIssuerType.Windows:
                                userName = SPClaimProviderManager.Local.ConvertClaimToIdentifier(loginName);
                                break;
                            default:
                                break;
                        }
                    }
                    else
                    {
                        userName = people.LoginName;
                    }
                }
            }

            return userName;
        }

In sharepoint 2013 you can get user login name by using:

SPClaimProviderManager claimManager = SPClaimProviderManager.Local;
		SPClaim userClaim = claimManager.ConvertSPUserToClaim(user);
		string loginName = claimManager.ConvertClaimToIdentifier(userClaim.ToEncodedString()); // given a Windows user encoded claim, this method will return a string in the DOMAIN\USERNAME format.

Articles for reference:

How Claims encoding works in SharePoint 2010 – Writen by Wictor Wilen, SharePoint MCA, MCSM, MCM and MVP

SPClaimProviderManager Methods by Microsoft

 

One Response

  1. translate google apis
    translate google apis at |

    It’s awesome to pay a visit this site and reading the views of
    all friends regarding this piece of writing, while I am also keen of getting know-how.

Leave a Reply