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:
- 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]
- 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]
- 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:
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)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)- 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
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