Blazor WebAssembly中AuthorizationMessageHandler无法解析IAccessTokenProvider求助
IAccessTokenProvider服务 我是Blazor技术新手,开发.NET 6 Blazor WebAssembly客户端项目时,遇到AuthorizationMessageHandler无法解析类型为Microsoft.AspNetCore.Components.WebAssembly.Authentication.IAccessTokenProvider的服务的异常。
已安装的包
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.13" /> <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="6.0.13" /> <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.13" PrivateAssets="all" /> <PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
GraphAuthorizationMessageHandler.cs代码
public class GraphAuthorizationMessageHandler : AuthorizationMessageHandler { public GraphAuthorizationMessageHandler(IAccessTokenProvider provider, NavigationManager navigation, IConfiguration config) : base(provider, navigation) { ConfigureHandler( authorizedUrls: new[] { config.GetSection("MicrosoftGraph")["BaseUrl"] }, scopes: config.GetSection("MicrosoftGraph:Scopes").Get<List<string>>()); } }
Program.cs代码
using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; using MySample.WebAssembly.Client; var builder = WebAssemblyHostBuilder.CreateDefault(args); builder.RootComponents.Add<App>("#app"); builder.RootComponents.Add<HeadOutlet>("head::after"); builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); builder.Services.AddTransient<GraphAuthorizationMessageHandler>(); builder.Services.AddHttpClient("GraphAPI", client => client.BaseAddress = new Uri( builder.Configuration.GetSection("MicrosoftGraph")["BaseUrl"])) .AddHttpMessageHandler<GraphAuthorizationMessageHandler>(); await builder.Build().RunAsync();
异常信息
Unhandled exception rendering component: Unable to resolve service for type 'Microsoft.AspNetCore.Components.WebAssembly.Authentication.IAccessTokenProvider' while attempting to activate 'MySample.WebAssembly.Client.GraphAuthorizationMessageHandler'.
问题核心是DI容器中未注册Blazor WebAssembly认证服务,IAccessTokenProvider由Microsoft.AspNetCore.Components.WebAssembly.Authentication包提供,必须通过专属的认证注册方法注入容器。
步骤1:添加认证服务注册
在Program.cs中,注册HttpClient和GraphAuthorizationMessageHandler之前,添加认证服务代码。根据你使用的认证类型选择对应方法:
- 若使用OIDC认证(如微软账号、Azure AD):
builder.Services.AddOidcAuthentication(options => { // 从appsettings.json绑定认证配置,如Authority、ClientId等 builder.Configuration.Bind("AzureAd", options.ProviderOptions); });
- 若配合ASP.NET Core Identity Server使用API授权:
builder.Services.AddApiAuthorization();
步骤2:调整代码顺序
确保认证服务注册在HttpClient和消息处理器之前,修改后的完整Program.cs示例:
using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; using Microsoft.AspNetCore.Components.WebAssembly.Authentication; using MySample.WebAssembly.Client; var builder = WebAssemblyHostBuilder.CreateDefault(args); builder.RootComponents.Add<App>("#app"); builder.RootComponents.Add<HeadOutlet>("head::after"); // 先注册认证服务 builder.Services.AddOidcAuthentication(options => { builder.Configuration.Bind("AzureAd", options.ProviderOptions); }); builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); builder.Services.AddTransient<GraphAuthorizationMessageHandler>(); builder.Services.AddHttpClient("GraphAPI", client => client.BaseAddress = new Uri( builder.Configuration.GetSection("MicrosoftGraph")["BaseUrl"])) .AddHttpMessageHandler<GraphAuthorizationMessageHandler>(); await builder.Build().RunAsync();
说明
AddOidcAuthentication或AddApiAuthorization会自动注册IAccessTokenProvider及其他认证相关服务,让DI容器能找到该服务并注入到GraphAuthorizationMessageHandler中。同时需确保appsettings.json中配置了正确的认证参数(如Authority、ClientId),参数配置可参考对应认证服务的官方文档要求。
内容的提问来源于stack exchange,提问作者Ziggler

