如何解决Blazor自定义身份验证消息处理程序
这是我的blazor wasm项目的配置
builder.Services.AddOptions();
builder.Services.AddAuthorizationCore();
builder.Services.AddScoped<AuthStateProvider>();
builder.Services.AddScoped<AuthenticationStateProvider>(s => s.GetRequiredService<AuthStateProvider>());
builder.Services.AddScoped<AuthenticationService>(s => webcontext.Authentication);
builder.Services.AddTransient<AuthMessageHandler>();
builder.Services.AddHttpClient("ServerAPI",client => client.BaseAddress = AppModelContext.BaseUri).AddHttpMessageHandler<AuthMessageHandler>();
builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("ServerAPI"));
和登录页面
[Inject]
public IHttpClientFactory ClientFactory { get; set; }
private async void Login()
{
try
{
var Client = ClientFactory.CreateClient("ServerAPI");
var LoginResult = await Client.GetAsync("WebApis/Login/UserName=e.vali&Password=111");
}
catch (AccessTokenNotAvailableException exception)
{
exception.Redirect();
}
}
当我运行项目时,
var Client = ClientFactory.CreateClient("ServerAPI");
行 抛出该异常
Unhandled Exception:System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Components.WebAssembly.Authentication.IAccessTokenProvider' while attempting to activate 'AppModel.Services.Authentication.AuthMessageHandler'.
如何注册IAccesstokenProvider服务?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。