C# 使用autofac识别服务器3
我正在尝试将IdentityServer3实现到使用Autofac的现有项目中。我遇到的问题是,在设置自定义服务时,如果我运行项目并尝试进行身份验证,则会出现以下错误: “尝试创建“TokenEndpointController”类型的控制器时出错。请确保该控制器具有无参数公共构造函数。” 现在我知道,当服务未正确设置时,这是一个通用的autofac错误。 这个错误实际上是在抱怨我的自定义UserService声明: 在类型“Business.IdentityServer.IdentityServerUserService”上使用“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”找到的构造函数都不能使用可用的服务和参数调用: 无法解析构造函数“Void.ctor(Business.Providers.IUserProvider)”的参数“Business.Providers.IUserProvider userProvider” 现在,在我开始使用IdentityServer3之前,我已经有了一个用户提供程序,它是在autofac中设置的,如下所示:C# 使用autofac识别服务器3,c#,autofac,identityserver3,C#,Autofac,Identityserver3,我正在尝试将IdentityServer3实现到使用Autofac的现有项目中。我遇到的问题是,在设置自定义服务时,如果我运行项目并尝试进行身份验证,则会出现以下错误: “尝试创建“TokenEndpointController”类型的控制器时出错。请确保该控制器具有无参数公共构造函数。” 现在我知道,当服务未正确设置时,这是一个通用的autofac错误。 这个错误实际上是在抱怨我的自定义UserService声明: 在类型“Business.IdentityServer.IdentitySer
builder.RegisterType<DatabaseContext>().As<DbContext>().InstancePerDependency();
builder.RegisterType<UserProvider>().As<IUserProvider>().InstancePerDependency();
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;
}
builder.RegisterType().As().InstancePerDependency();
builder.RegisterType().As().InstancePerDependence();
这在以前是有效的,所以我知道用户提供者实际上有它的所有依赖项
我的用户服务如下所示:
builder.RegisterType<DatabaseContext>().As<DbContext>().InstancePerDependency();
builder.RegisterType<UserProvider>().As<IUserProvider>().InstancePerDependency();
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;
}
公共类标识ServerUserService:UserServiceBase
{
专用只读IUserProvider\u用户提供程序;
公共标识服务器用户服务(IUserProvider userProvider)
{
_userProvider=userProvider;
}
公共覆盖异步任务AuthenticateLocalAsync(LocalAuthenticationContext)
{
var user=await\u userProvider.FindAsync(context.UserName,context.Password);
if(user!=null&&!user.Disabled)
{
//获取用户声明
//将用户添加到我们的上下文中
context.AuthenticateResult=新的AuthenticateResult(user.Id,user.UserName,new List());
}
}
}
有人知道我如何解决这个问题吗?这是因为我是如何配置工厂的。我现在有这样一个:
builder.RegisterType<DatabaseContext>().As<DbContext>().InstancePerDependency();
builder.RegisterType<UserProvider>().As<IUserProvider>().InstancePerDependency();
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;
}
private static IdentityServerServiceFactory配置(此IdentityServerServiceFactory工厂,CormarConfig配置)
{
var servicepoptions=new EntityFrameworkServiceOptions{ConnectionString=config.SqlConnectionString};
注册操作服务(服务选项);
工厂注册表配置服务(服务选项);
factory.CorsPolicyService=new Registration(new DefaultCorsPolicyService{AllowAll=true});//允许所有域访问身份验证
工厂注册(新注册(dr=>dr.ResolveFromAutofacOwinLifetimeScope());
factory.UserService=新注册(dr=>dr.ResolveFromAutofacOwinLifetimeScope());
factory.ClientStore=新注册(dr=>dr.ResolveFromAutofacOwinLifetimeScope());
factory.ScopeStore=新注册(dr=>dr.ResolveFromAutofacOwinLifetimeScope());
返回工厂;
}
我的用户服务还是一样的,所以一切正常。我也有这个问题。我看不到IUserProvider在哪里注册?您注册DbContext的方式的改变是否有意义?IUserProvider是我自己为
用户管理器
实现的,我在autofac模块中注册了它,IUserProvider不是接口吗?也许你的意思是UserManager是IUserProvider的一个实现,但我不确定UserManager是什么。是的,它是一个接口,如果你创建一个新项目,它会称之为ApplicationUserManager,但我讨厌这个名称。以下是一篇文章: