【问题标题】:Why could not my Blazor project consume MyProj.HttpApi.Client correctly?为什么我的 Blazor 项目不能正确使用 MyProj.HttpApi.Client?
【发布时间】:2020-01-11 09:50:06
【问题描述】:

我使用 ABP CLI 生成了一个 MVC 模板,我想用它来尝试一个 Blazor Server 项目。我确实添加了一个与每个常见模块相同的 MyProjBlazorModule,就像 ConsoleTestApp 项目 所做的那样:

namespace MyProj.Blazor
{
    [DependsOn(
        typeof(MyProjHttpApiClientModule),
        typeof(AbpHttpClientIdentityModelModule)
        )]
    public class MyProjBlazorModule : AbpModule
    {
    }
}

然后我将模块作为服务添加到 ConfigureServices 方法:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSyncfusionBlazor();
        services.AddRazorPages();
        services.AddServerSideBlazor();
        services.AddSingleton<WeatherForecastService>();
        services.AddApplication<TaurusBlazorModule>();

    }

为了进行快速测试,我还从模板项目 MyProj.HttpApi.Client.ConsoleTestApp 中复制了 ClientDemoService 类,并在 index.razor 中使用它,如下所示:

@inject ClientDemoService _clientService
...
protected override async Task OnInitializedAsync()
{

    await base.OnInitializedAsync();
    profile = await _clientService.RunAsync();
}

但它无法工作,浏览器中出现错误消息:

InvalidOperationException:没有指定 authenticationScheme,也没有找到 DefaultAuthenticateScheme。默认方案可以 使用 AddAuthentication(string defaultScheme) 或 AddAuthentication(Action configureOptions)。

如果我像这样复制与控制台测试项目相同的代码:

        using (var application = AbpApplicationFactory.Create<MyProjConsoleApiClientModule>())
        {
            application.Initialize();

            var demo = application.ServiceProvider.GetRequiredService<ClientDemoService>();
            profile = AsyncHelper.RunSync(() => demo.RunAsync());

        }

它奏效了。我想知道在这里使用 ABP 模块和显式调用丑陋的 ServiceProvider 方法之间的区别,以及如何以正确而美观的方式解决这个问题?

感谢大家的帮助!

【问题讨论】:

  • 这个错误意味着你设置了身份验证:services.AddAuthentication
  • 是的,我试过了,比如:services.AddAuthentication(o => { o.DefaultScheme = "TokenAuthenticationScheme"; });但问题是一样的......
  • 但这还不够,您还需要设置一个身份验证处理程序(cookie、jwt、oidc 等)并在您的管道中设置身份验证中间件app.UseAuthentication():docs.microsoft.com/en-us/aspnet/core/security/authentication/…跨度>
  • 我会研究的。谢谢,但是为什么控制台演示不需要这样的配置并且运行良好?是“application.ServiceProvider.GetRequiredService();”除了模块注册之外,还有什么神奇的事情吗?
  • 因为它为您设置了身份验证。

标签: asp.net-core .net-core blazor abp


【解决方案1】:

最后,我知道这有什么问题。在 abp CLI 的模板源代码中,MyProjHttpApiHostModule 的 ConfigureAuthentication 方法注册身份验证服务如下:

    private void ConfigureAuthentication(ServiceConfigurationContext context, IConfiguration configuration)
    {
        context.Services.AddAuthentication()
            .AddIdentityServerAuthentication(options =>
            {
                options.Authority = configuration["AuthServer:Authority"];
                options.RequireHttpsMetadata = false;
                options.ApiName = "MyProj";
                options.JwtBackChannelHandler = new HttpClientHandler()
                {
                    ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
                };
            });
    }

其中 AddAuthentication() 方法使用了 空参数 重载,导致 No authenticationScheme is specified 错误。我引用了IdentityServer4 official document 并找到了正确的方法:

context.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddIdentityServerAuthentication(options =>
            {
                ...
            });

很简单,我应该设置默认方案JwtBearerDefaults.AuthenticationScheme 正如错误所报告的那样,使用不同的 AddAuthentication 方法重载。

我希望这篇文章可以帮助面临相同或类似问题的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-24
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多