【问题标题】:IdentityServer3 with autofac带有 autofac 的 IdentityServer3
【发布时间】:2017-09-15 17:40:39
【问题描述】:

我正在尝试将 IdentityServer3 实施到使用 Autofac 的现有项目中。我遇到的问题是,当我设置自定义服务时,如果我运行我的项目并尝试进行身份验证,我会收到此错误:

“尝试创建 'TokenEndpointController' 类型的控制器时出错。请确保控制器具有无参数的公共构造函数。”

现在我知道,当服务设置不正确时,这是一个通用的 autofac 错误。 该错误实际上抱怨我的自定义 UserService 声明:

在“Business.IdentityServer.IdentityServerUserService”类型上使用“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”找到的任何构造函数都不能使用可用的服务和参数调用: 无法解析构造函数“Void .ctor(Business.Providers.IUserProvider)”的参数“Business.Providers.IUserProvider userProvider”。

现在我在开始使用 IdentityServer3 之前已经有一个 UserProvider,它是在 autofac 中设置的,如下所示:

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

这是以前工作的,所以我知道 UserProvider 实际上确实具有它的所有依赖项。

我的用户服务如下所示:

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

有谁知道我该如何解决这个问题?

【问题讨论】:

    标签: c# autofac identityserver3


    【解决方案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;
        }
    

    我的用户服务还是一样的,所以一切正常。

    【讨论】:

    • 我也有这个问题。我没有看到 IUserProvider 在哪里注册?更改注册 DbContext 的方式是否有意义?
    • IUserProvider 是我自己对 UserManager 的实现,我在 autofac 模块中注册了它
    • IUserProvider 不是接口吗?也许你的意思是 UserManager 是 IUserProvider 的一个实现,但我不确定 UserManager 是什么。
    • 是的,它是一个接口,如果您创建一个新项目,它会调用它 ApplicationUserManager,但我讨厌这个名字。这是一篇文章:scottbrady91.com/ASPNET-Identity/…
    猜你喜欢
    • 1970-01-01
    • 2014-08-09
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 2016-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多