Facing Issue while getting user claims using ClaimsPrincipal

Nikhil Badhe 1 Reputation point
2020-11-30T11:16:15.663+00:00

Hi ,
I am using SharePoint 2013.

var principal = ClaimsPrincipal.Current;

Above code used to get users claim.
Newly Created group not getting in principal variable.

Can you please help, why newly created group not getting in principal variable.

Thanks.

SharePoint Server
SharePoint Server
A family of Microsoft on-premises document management and storage systems.
2,346 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Michael Taylor 54,321 Reputation points
    2020-11-30T15:26:08.793+00:00

    User group memberships are cached. If you change the groups a user is a member of then they generally need to log out and back in again to refresh the group memberships.

    I've not confirmed nor have I ever heard that Windows was updated to have the group memberships periodically updated, like GPOs, so this is the only way I know to refresh group membership.

    0 comments No comments

  2. Jerryzy 10,571 Reputation points
    2020-12-01T05:54:48.583+00:00

    Hi @NikhiBadhe-0141,

    You can get all group claims information like this:

    protected void Page_Load(object sender, EventArgs e)  
            {  
      
                try  
                {  
      
                    // Get the claims  
                     var cp = Page.User as IClaimsPrincipal;  
                    if (cp != null)  
                    {  
                        DataRow dr;  
                        DataTable claimsTable = new DataTable();  
                        claimsTable.Columns.Add("Type", typeof(string));  
                        claimsTable.Columns.Add("Value", typeof(string));  
      
                        IClaimsIdentity ci = (IClaimsIdentity)cp.Identity;  
                        foreach (Claim c in ci.Claims)  
                        {  
                            dr = claimsTable.NewRow();  
                            dr["Type"] = c.ClaimType.ToString();  
                            dr["Value"] = c.Value.ToString();  
                            claimsTable.Rows.Add(dr);  
                        }  
      
                        claimsGrid.DataSource = claimsTable;  
                        claimsGrid.DataBind();  
                    }  
                }  
                catch (Exception ex)  
                {  
                    this.Controls.Add(new LiteralControl(ex.Message));  
                }  
            }  
    

    43981-snipaste-2020-12-01-13-44-23.png

    Read Claims Of A User In SharePoint 2013


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. sadomovalex 3,631 Reputation points
    2020-12-07T15:02:38.633+00:00

    the code above will work only when you are logged in under account of newly created user. If code runs under different account you need to get reference on this user from the web first:
    string loginName = "...";
    var user = web.EnsureUser(loginName);

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.