0

I am working with the IdentityUser class [Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUser], and would like to extend (or override) the IdentityUser class to allow null(able) claim values, but am having a hard time trying to figure out how to override the class and create the logic to allow this. Please let me know if anyone can assist.

Thank you.

This is the code I have started, but not sure how to get it implemented correctly, and or what additional logic needs to be changed/added to make it work.

public class CustomUser : IdentityUser
{
    private ICollection<CustomClaim<string>> _claims { get; set; }

    public CustomUser()
    {
        _claims = new List<CustomClaim<string>>();
    }

    public override ICollection<IdentityUserClaim<string>> Claims
    {
        get => _claims;
    }

    public class CustomClaim<TKey> : IdentityUserClaim<string>
    {
        public override string ClaimValue
        {
            get {
                if (base.ClaimValue != null)
                {
                    return base.ClaimValue;
                }
                else
                {
                    return "null";
                }
            }
            set => base.ClaimValue = value;
        }
    }
}
CodingRiot
  • 135
  • 1
  • 3
  • 7
  • Why nest the class CustomClaim in the entity CustomUser? Remove CustomClaim and add the GenerateUserIdentityAsync method. This will add some specific claims and available roles. You can add additional claims in ApplicationOAuthProvider.GrantResourceOwnerCredentials, where the token is returned. –  May 03 '17 at 23:29
  • Why do you need claims with a `null` value? Can you distinguish between having a `null`'ed or a non-existing named claim? – ckerth May 24 '17 at 15:09

0 Answers0