Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(ClaimsIdentity) duplicate custom claims

From brockallen's article, He says that the "AuthenticateAsync() might be invoked multiple times" which could be the reason why the TransformAsync() is being called more than once (twice on my app).

What I don't get is:

  1. When I construct ClaimsIdentity WITH parameters, the duplication only happens on added claims ('now' and 'boom'). [See Code 1, Image 1-A and Image 1-B below]
  2. When I construct ClaimsIdentity WITHOUT parameters, the added claims ('now' and 'boom') does not duplicate. [See Code 2, Image 2-A and Image 2-B below]
  3. How come the added claims (now and boom) are duplicated WHILE the other/pre-defined claims (nbf, exp, iss, aud, etc...) are not? [Compare Image 1-A and Image 1-B below]

Does anybody know why the ClaimsIdentity behaves this way?


UPDATED QUESTION:

What I don't get is:

  1. When I construct ClaimsIdentity WITH parameters, the duplication only happens on added claims ('now' and 'boom'). [See Code 1, Image 1-A and Image 1-B below]
    (Answered)
  2. When I construct ClaimsIdentity WITHOUT parameters, the added claims ('now' and 'boom') does not duplicate. [See Code 2, Image 2-A and Image 2-B below]
    (Answered)
  3. How come the added claims (now and boom) are duplicated WHILE the other/pre-defined claims (nbf, exp, iss, aud, etc...) are not? [Compare Image 1-A and Image 1-B below]
    (Not Answered, but alternative code is posted below)

Code 1

class ClaimsTransformer : IClaimsTransformation
{
    public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
    {
        var id = ((ClaimsIdentity)principal.Identity);
        var ci = new ClaimsIdentity(id.Claims, id.AuthenticationType, id.NameClaimType, id.RoleClaimType);

        ci.AddClaim(new Claim("now", DateTime.Now.ToString()));
        ci.AddClaim(new Claim("boom", "hehehe"));

        var cp = new ClaimsPrincipal(ci);

        return Task.FromResult(cp);
    }
}

Image 1-A

enter image description here

Image 1-B

enter image description here


Code 2

class ClaimsTransformer : IClaimsTransformation
{
    public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
    {
        var ci = new ClaimsIdentity();

        ci.AddClaim(new Claim("now", DateTime.Now.ToString()));
        ci.AddClaim(new Claim("boom", "hehehe"));

        var cp = new ClaimsPrincipal(ci);

        return Task.FromResult(cp);
    }
}

Image 2-A

enter image description here

Image 2-B

enter image description here

like image 544
gulp Avatar asked Aug 07 '18 05:08

gulp


People also ask

What is the claimsidentity class?

The ClaimsIdentity class is a concrete implementation of a claims-based identity; that is, an identity described by a collection of claims. A claim is a statement about an entity made by an issuer that describes a property, right, or some other quality of that entity. Such an entity is said to be the subject of the claim.

What is claims identity in Windows?

Windows Identity The ClaimsIdentity class is a concrete implementation of a claims-based identity; that is, an identity described by a collection of claims. A claim is a statement about an entity made by an issuer that describes a property, right, or some other quality of that entity.

Why is my claimsidentity name property not set?

By default, ClaimsIdentity gets that Name property value from a claim with the claim type of ClaimTypes.Name. If you didn’t set that value or you didn’t set that value properly in the list of Claims you passed in, then that Name property won’t get set.

How do I access the claims of an individual claimsidentity?

In the majority of cases you should access the user's claims through the ClaimsPrincipal.Claims collection rather than through the Claims collection. You will need to access the claims of an individual ClaimsIdentity only in the cases where the principal contains more than one ClaimsIdentity and you need to evaluate or modify a specific identity.


1 Answers

Answering Question 1:
I realized that duplication happens because i am copying the values from the principal and returns back with the added custom claims.

Answering Question 2:
I realized that duplication NEVER happens because i am always creating a new ClaimsIdentity and never copies the values from the principal.

like image 168
gulp Avatar answered Sep 27 '22 16:09

gulp