2017-08-31 5 views
1

Autofacを使用する既存のプロジェクトにIdentityServer3を実装しようとしています。Autofacを使用したIdentityServer3

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

は、今私は、サービスがある場合、これは、一般的なautofacエラーです知っている:私が遭遇した問題は、私は私のカスタムサービスを設定するとき、私は私のプロジェクトを実行し、認証しようとした場合、私はこのエラーを取得するということです正しく設定されていません。 エラーが実際に知らせる私のカスタムUserServiceのについてうめき声:私はIdentityServer3を使用し始め、それがこのようautofacに設立された前に、今、私はすでにUserProviderを持っていた

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)'.

を:

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>()); 
     } 
    } 
} 

は、誰もが、私はこの問題を解決する方法を知っていますか?

答えて

1

これは私が工場をどのように設定していたかによるものです。私は今このようにしています:

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; 
    } 

私のユーザサービスはまだ同じですので、すべてが機能します。

関連する問題