首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IdentityServer3带autofac

IdentityServer3带autofac
EN

Stack Overflow用户
提问于 2017-08-31 12:33:47
回答 1查看 526关注 0票数 1

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

“试图创建类型为”TokenEndpointController“的控制器时发生了错误。请确保控制器具有无参数的公共构造函数。”

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

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

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

代码语言:javascript
复制
builder.RegisterType<DatabaseContext>().As<DbContext>().InstancePerDependency();
builder.RegisterType<UserProvider>().As<IUserProvider>().InstancePerDependency();

这是以前起作用的,所以我知道UserProvider实际上拥有它的所有依赖项。

我的UserService看起来是这样的:

代码语言:javascript
复制
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>());
        }
    }
}

有人知道我怎么解决这个问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-15 09:40:39

这是因为我是如何配置工厂的。我现在有这样的想法:

代码语言:javascript
复制
    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;
    }

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45981572

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档