User1724605321 posted
Hi doorwaar ,
Please refer to below code sample for how to build an MVC web application that performs identity management with Azure AD B2C using the ASP.Net Core OpenID Connect middleware:
https://github.com/Azure-Samples/active-directory-b2c-dotnetcore-webapp
In that app , to call web api , you just need to modify the OnAuthorizationCodeReceived function to use code to get the access token form Azure AD's token endpoint using MSAL :
public async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedContext context)
{
// Use MSAL to swap the code for an access token
// Extract the code from the response notification
var code = context.ProtocolMessage.Code;
string signedInUserID = context.Principal.FindFirst(ClaimTypes.NameIdentifier).Value;
TokenCache userTokenCache = new MSALSessionCache(signedInUserID, context.HttpContext).GetMsalCacheInstance();
ConfidentialClientApplication cca = new ConfidentialClientApplication(AzureAdB2COptions.ClientId, AzureAdB2COptions.Authority, AzureAdB2COptions.RedirectUri, new ClientCredential(AzureAdB2COptions.ClientSecret), userTokenCache, null);
try
{
AuthenticationResult result = await cca.AcquireTokenByAuthorizationCodeAsync(code, AzureAdB2COptions.ApiScopes.Split(' '));
context.HandleCodeRedemption(result.AccessToken, result.IdToken);
}
catch (Exception ex)
{
//TODO: Handle
throw;
}
}
Best Regards,
Nan Yu