Friday, June 15, 2012

C# code to get claims user name sharepoint 2010


 

IClaimsPrincipal claimsPrincipal = Page.User as IClaimsPrincipal;
                    if (claimsPrincipal != null)
                    {
                        IClaimsIdentity claimsIdentity = (IClaimsIdentity)Thread.CurrentPrincipal.Identity;
                        if (claimsIdentity != null)
                        {
                             foreach (Claim item in claimsIdentity.Claims)
                          {
                           }
                        }

                     }

2 comments:

  1. how to get the claims of a different user? Let say as a admin i want to see the claims of other user by passing userID

    ReplyDelete
    Replies
    1. Suppose you have a user as SPUser object convert that SPUser object into IClaimsPrincipal.

      That means replace the first line of my code from Page.User as IClaimsPrincipal to SPUser object as IClaimsPrincipal

      NOTE : make sure that the object should not be null or wrongly type casted before you process otherwise it will throw error

      Delete