IdentityServer3 with autofac

问题描述:

我正在尝试将IdentityServer3实现为使用Autofac的现有项目。我所遇到的问题是,当我建立了我的定制服务,如果我跑我的项目,并尝试验证我得到这个错误:IdentityServer3 with autofac

"An error occurred when trying to create a controller of type 'TokenEndpointController'. Make sure that the controller has a parameterless public constructor."

现在我知道这是一个通用autofac错误,当一个服务有没有正确设置。 错误实际上呻吟关于我的自定义UserService指出:

None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Business.IdentityServer.IdentityServerUserService' can be invoked with the available services and parameters: Cannot resolve parameter 'Business.Providers.IUserProvider userProvider' of constructor 'Void .ctor(Business.Providers.IUserProvider)'.

现在,我已经有了一个UserProvider之前,我开始使用IdentityServer3,它成立于autofac这样的:

builder.RegisterType<DatabaseContext>().As<DbContext>().InstancePerDependency(); 
builder.RegisterType<UserProvider>().As<IUserProvider>().InstancePerDependency(); 

这是以前的工作,所以我知道UserProvider确实具有所有的依赖关系。

我UserService看起来是这样的:

public class IdentityServerUserService : UserServiceBase 
{ 
    private readonly IUserProvider _userProvider; 

    public IdentityServerUserService(IUserProvider userProvider) 
    { 
     _userProvider = userProvider; 
    } 

    public override async Task AuthenticateLocalAsync(LocalAuthenticationContext context) 
    { 
     var user = await _userProvider.FindAsync(context.UserName, context.Password); 

     if (user != null && !user.Disabled) 
     { 
      // Get the UserClaims 

      // Add the user to our context 
      context.AuthenticateResult = new AuthenticateResult(user.Id, user.UserName, new List<Claim>()); 
     } 
    } 
} 

有谁知道我怎样才能解决这个问题呢?

这是由于我是如何配置工厂的。我现在有这样的:

private static IdentityServerServiceFactory Configure(this IdentityServerServiceFactory factory, CormarConfig config) 
    { 
     var serviceOptions = new EntityFrameworkServiceOptions { ConnectionString = config.SqlConnectionString }; 
     factory.RegisterOperationalServices(serviceOptions); 
     factory.RegisterConfigurationServices(serviceOptions); 

     factory.CorsPolicyService = new Registration<ICorsPolicyService>(new DefaultCorsPolicyService { AllowAll = true }); // Allow all domains to access authentication 
     factory.Register<DbContext>(new Registration<DbContext>(dr => dr.ResolveFromAutofacOwinLifetimeScope<DbContext>())); 
     factory.UserService = new Registration<IUserService>(dr => dr.ResolveFromAutofacOwinLifetimeScope<IUserService>()); 
     factory.ClientStore = new Registration<IClientStore>(dr => dr.ResolveFromAutofacOwinLifetimeScope<IClientStore>()); 
     factory.ScopeStore = new Registration<IScopeStore>(dr => dr.ResolveFromAutofacOwinLifetimeScope<IScopeStore>()); 

     return factory; 
    } 

我的用户的服务仍然是相同的,所以一切正常。