I am following this tutorial from MSFT:
Based on the following code I should be able inject the IAuthorizationHeaderProvider
and get the token:
[Authorize]
public class HomeController : Controller
{
readonly IAuthorizationHeaderProvider authorizationHeaderProvider;
public HomeController(IAuthorizationHeaderProvider authorizationHeaderProvider)
{
this.authorizationHeaderProvider = authorizationHeaderProvider;
}
// Code for the controller actions (see code below)
}
Then use IAuthorizationHeaderProvider
in the controller:
[AuthorizeForScopes(Scopes = new[] { "user.read" })]
public async Task<IActionResult> Profile()
{
// Acquire the access token.
string[] scopes = new string[]{"user.read"};
string accessToken = await
authorizationHeaderProvider.CreateAuthorizationHeaderForUserAsync(scopes);
...
}
Also per the documentation:
Your web app needs to acquire a token for the downstream API. You specify it by adding the .EnableTokenAcquisitionToCallDownstreamApi() line after .AddMicrosoftIdentityWebApp(Configuration). This line exposes the IAuthorizationHeaderProvider service that you can use in your controller and page actions. However, as you see in the following two options, it can be done more simply. You also need to choose a token cache implementation, for example .AddInMemoryTokenCaches(), in Startup.cs:
I have added this code in Startup.cs
:
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration, "AzureAd")
.EnableTokenAcquisitionToCallDownstreamApi(new string[]{"user.read" })
.AddInMemoryTokenCaches();
However, I get the following error when running the project:
Unable to resolve service for type 'Microsoft.Identity.Abstractions.IAuthorizationHeaderProvider' while attempting to activate
First of all, this exception should relate to DI, at first I was thinking about that it might due to an old version of Microsoft.Identity.Web
, however when I test with version 1.16.0, I got compile exception. If I upgrade to the latest stable version, everything worked fine and failed to reproduce your issue.
Here's my test result.
I think you might first checking the nuget packages version, upgrade to the lateset and rebuild your app. Here's mine:
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.10" NoWarn="NU1605" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="7.0.10" NoWarn="NU1605" />
<PackageReference Include="Microsoft.Identity.Web" Version="2.13.4" />
<PackageReference Include="Microsoft.Identity.Web.UI" Version="2.13.4" />
</ItemGroup>
Or you might create a new project with the VS template and choose Microsoft Identity platform as the Authentication type. Then modify the Program.cs and your controller with the code snippet you shared in the question, then fill the fields in the appsettings.json with the value you got from Azure AD. Do not forget to add the ClientSecret, the default template doesn't provide the ClientSecret
field.