使用AutoFac解决.Net Core 2.0中的身份验证服务

Tal*_*aul 5 asp.net dependency-injection autofac asp.net-core-2.0

我正在将.Net Core 1.x项目迁移到.Net Core 2.0。发生的变化之一是,现在ConfigureServices在启动期间使用扩展名来配置身份验证IServiceCollection

我的身份验证方案中使用了一些自定义服务,但是大部分DI注册都是使用AutoFac构建的(在此之后被调用):

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    //Other .Net Services Registered
    services.AddTransient<ITicketStore, DistributedCacheTicketStore>(); 
    services.AddSingleton<AuthenticationEvents>();  

    var sp = services.BuildServiceProvider();

    services.AddAuthentication()
        .AddCookie(options =>
        {
            //Other cookie options
            options.SessionStore = sp.GetService<ITicketStore>();
        })
        .AddOpenIdConnect(options =>
        {
            //Other OIDC options
            options.Events = sp.GetService<AuthenticationEvents>();
        });

    //Register application services with AutoFac
    var builder = new ContainerBuilder();
    RegisterAutoFacTypes(builder);

    //Include services from .Net DI container
    builder.Populate(services);

    //Build and return the AutoFac container
    var container = builder.Build();
    return container.Resolve<IServiceProvider>();
}
Run Code Online (Sandbox Code Playgroud)

目前,我试图同时注册的依赖关系DistributedCacheTicketStore,并AuthenticationEventsIServiceCollection让我在我的验证配置使用它们,但这是越来越乱,我宁愿保持它在AutoFac注册。

有没有一种明智的重构方法,可以将这些注册信息保留在AutoFac中,但是services.AddAuthentication()在构建AutoFac容器之前仍可以使用?