What I'm trying to do is add a claim after authentication. The following example of triggering an OnTokenValidation event not actually working.
I'm using Microsoft.Identity.Web to authenticate on Azure AD. That part works! How can I register events using AddMicrosoftIdentityWebAppAuthentication to add custom claims
services.AddMicrosoftIdentityWebApiAuthentication(_configuration);
services.Configure<MicrosoftIdentityOptions>(OpenIdConnectDefaults.AuthenticationScheme, options =>
{
options.Events = new OpenIdConnectEvents
{
OnTokenValidated = ctx =>
{
var claims = new List<Claim> {
new Claim(ClaimTypes.OtherPhone, "somevalue")
};
ctx.Principal.AddIdentity(new ClaimsIdentity(claims));
return Task.CompletedTask;
},
};
});
You are using AddMicrosoftIdentityWebApiAuthentication, so the events that will be triggered are JwtBearerEvents.
You can set them up as below (.NET 6 API):