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