'Using MSAL to obtain tokens for MS Graph without overriding custom app authentication

I'm building a .NET 6 ASP.NET app that has custom authentication. I need to call MS Graph APIs on behalf of my users and I want to use MSAL for that. Unfortunately, all examples include signing in the users to the app via Azure AD. I do not want MSAL to override my custom authentication, I just want to obtain the access/refresh tokens and I will tie them in with my local user principals.

This is the example code I'm looking to modify:

builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"))
    .EnableTokenAcquisitionToCallDownstreamApi(new[] { "User.Read" })
    .AddMicrosoftGraph(builder.Configuration.GetSection("Graph"))
    .AddInMemoryTokenCaches();

I could manually craft all the OAuth 2.0 messages but surely MSAL can do that for me without overriding my app's User.Current? Any tips, examples?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source