Top Menu

Friday, December 6, 2013

SharePoint 2013 - Claim Information in a Web Part

I ran into a situation where I have to see what is the claims information for the current SharePoint user. You have to have Windows Identity Foundation (WIF) 4.5 installed on the SharePoint server in order to run the code.

Following is the updated version of the code I found on MSDN:

Ref: http://msdn.microsoft.com/en-us/library/ee517271.aspx
using Microsoft.IdentityModel.Claims;

protected override void CreateChildControls()
{
try
  {
      this.Controls.Add(new LiteralControl(GetClaimInfo()));
  }
  catch (System.Exception exp)
  {
       this.Controls.Add(new LiteralControl(exp.Message));
  }
}

public string GetClaimInfo()
{
    String strClaim = null;
    IClaimsPrincipal icp = Page.User as IClaimsPrincipal;

    // Access IClaimsIdentity which contains claims
    IClaimsIdentity claimsIdentity = (IClaimsIdentity)icp.Identity;

            
    // Access claims
    foreach (Claim claim in claimsIdentity.Claims)
    {
     strClaim = claim.Issuer + " - " + claim.ClaimType + " - " + claim.Value + "<br/>" + strClaim;
    }
    return strClaim;
}
Here is another version of the code to get all the AD groups for a user:
public string GetClaimInfo()
        {
            String strClaim = null;
            string strGroup = null;

            IClaimsPrincipal icp = Page.User as IClaimsPrincipal;

            // Access IClaimsIdentity which contains claims
            IClaimsIdentity claimsIdentity = (IClaimsIdentity)icp.Identity;

            
            // Access claims
            foreach (Claim claim in claimsIdentity.Claims)
            {
                if (claim.ClaimType.EndsWith("groupsid"))
                {
                    strGroup = new System.Security.Principal.SecurityIdentifier(claim.Value).Translate(typeof(System.Security.Principal.NTAccount)).ToString();
                    strClaim = claim.Value + " - " + strGroup + "<br/> " + strClaim;
                }
            }
            return strClaim;
        }

No comments:

Post a Comment

Official SharePoint Documentation

I have recently contributed to the official SharePoint documentation for developement. Check it out here: https://docs.microsoft.com/en-us...