locked
ClaimsIdentity.FindFirst does not find claims RRS feed

  • Question

  • User657329123 posted

    Hi there,

    I'm adding a claim like this in my controller

    UserPrincipal principal = UserPrincipal.FindByIdentity(context, model.UserName);
    firstName = principal.GivenName;
    await UserManager.AddClaimAsync(user.Id, new Claim("first_name", firstName));

    In my View, I'm trying to access first_name claim like this

    @Html.ActionLink("Hello " + ((ClaimsIdentity)User.Identity).FindFirst("first_name").Value + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })

    but getting exception System.NullReferenceException: 'Object reference not set to an instance of an object.' In my view, I have added

    @using Microsoft.AspNet.Identity;
    @using System.Security.Claims;

    What am I doing wrong?

    Joe

    Tuesday, April 24, 2018 12:58 PM

All replies

  • User475983607 posted

    joegreen2005

    What am I doing wrong?

    Claims are added to the auth token which lives in a cookie.  Claims are added to the auth cookie during sign in.   Your code is adding the claim to the user store not the principal in the current context.

    More importantly you already have access to the GivenName property on the principal.  Why you adding the GivenName to a claim?

    Tuesday, April 24, 2018 1:49 PM
  • User657329123 posted

    You are correct not sure why I'm adding GivenName to a Claim. I wrote a helper in view like this and I'm getting the GivenName now.

            @helper AccountName()
            {
    
                    using (var context = new PrincipalContext(ContextType.Domain))
                    {
                        var principal = UserPrincipal.FindByIdentity(context, User.Identity.Name);
                        @principal.GivenName
                    }
            }
    
    
    @Html.ActionLink("Hello " + AccountName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { @title = "Manage", @class = "nav-link" })

    I also removed the Claim that I was adding. But can someone tell me how do you read the ClaimValue from View using Razor. I need to get UserId somehow first and then get the ClaimValue to query AspNetUserClaims table.

    Tuesday, April 24, 2018 2:38 PM
  • User1724605321 posted

    Hi joegreen :

    But can someone tell me how do you read the ClaimValue from View using Razor. I need to get UserId somehow first and then get the ClaimValue to query AspNetUserClaims table.

    Code below is for your reference :

    @{
        var claimsIdentity = User.Identity as System.Security.Claims.ClaimsIdentity;
    
        if (claimsIdentity != null)
        {
            var c = claimsIdentity.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier);
    
            if (c != null)
            {
                <p>
                    @c.Type.ToString();
                    @c.Value.ToString();
                </p>
            }
        }
    }

    Best Regards,

    Nan Yu

    Wednesday, April 25, 2018 5:09 AM