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:
Does anybody know why the ClaimsIdentity behaves this way?
UPDATED QUESTION:
What I don't get is:
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
Image 1-B
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
Image 2-B
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With