Skip to content

Instantly share code, notes, and snippets.

@leviwilson
Created October 18, 2011 17:16
Show Gist options
  • Save leviwilson/1296009 to your computer and use it in GitHub Desktop.
Save leviwilson/1296009 to your computer and use it in GitHub Desktop.
Getting User Guid From SharePoint
public class SharepointHelper
{
public static string UserName
{
get { return CurrentClaim.Value; }
}
private static SPClaim CurrentClaim
{
get { return SPClaimProviderManager.Local.DecodeClaim(SPContext.Current.Web.CurrentUser.LoginName); }
}
public static Guid UserId
{
get
{
if( IsWindowsAuthenticated)
{
using (HostingEnvironment.Impersonate())
{
var securityIdentifier =
SPActiveDirectoryClaimProvider.GetSecurityIdentifierFromClaim(CurrentClaim);
return new DirectoryEntry(string.Format("LDAP://<SID={0}>", securityIdentifier.Value)).Guid;
}
}
return (Guid) Membership.GetUser(UserName).ProviderUserKey;
}
}
public static bool IsWindowsAuthenticated
{
get { return SPOriginalIssuers.GetIssuerType(CurrentClaim.OriginalIssuer) == SPOriginalIssuerType.Windows; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment