There are some OpenIdConnectEvents which could be used to enable developer control over the authentication process.
For example , OnAuthorizationCodeReceived is invoked after security token validation if an authorization code is present in the protocol message. The event could be used to get access token for accessing API using authorization Code using ADAL/MSAL in Code/Hybrid Flow :
private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedContext context)
{
// Acquire a Token for the Graph API and cache it using ADAL. In the TodoListController, we'll use the cache to acquire a token for the Todo List API
string userObjectId = (context.Principal.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier"))?.Value;
var authContext = new AuthenticationContext(context.Options.Authority, new NaiveSessionCache(userObjectId, context.HttpContext.Session));
var credential = new ClientCredential(context.Options.ClientId, context.Options.ClientSecret);
var authResult = await authContext.AcquireTokenByAuthorizationCodeAsync(context.TokenEndpointRequest.Code,
new Uri(context.TokenEndpointRequest.RedirectUri, UriKind.RelativeOrAbsolute), credential, context.Options.Resource);
// Notify the OIDC middleware that we already took care of code redemption.
context.HandleCodeRedemption(authResult.AccessToken, context.ProtocolMessage.IdToken);
}
Code sample link : Calling a web API in an ASP.NET Core web application using Azure AD.
OnTokenValidated could be used to add custom claims to a user during authentication. Please check above document to get more events .