我在尝试将我的 UserManager 注入我的控制器时遇到问题。
这是我的自定义 UserManager:
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store, IOptions<IdentityOptions> optionsAccessor, IPasswordHasher<ApplicationUser> passwordHasher,
IEnumerable<IUserValidator<ApplicationUser>> userValidators,
IEnumerable<IPasswordValidator<ApplicationUser>> passwordValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors,
IServiceProvider services, ILogger<UserManager<ApplicationUser>> logger)
: base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
{
}
public async Task<ApplicationUser> FindAsync(string id, string password)
{
ApplicationUser user = await FindByIdAsync(id);
if (user == null)
{
return null;
}
return await CheckPasswordAsync(user, password) ? user : null;
}
}
这是我的 Startup.cs
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.ConfigureCors();
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = Configuration["Jwt:Issuer"],
ValidAudience = Configuration["Jwt:Issuer"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Key"]))
};
});
// Add framework services.
services.AddMvc();
services.AddScoped<ApplicationUserManager>();
var builder = new ContainerBuilder();
builder.Populate(services);
// Registering MongoContext.
builder.RegisterType<MongoContext>().AsImplementedInterfaces<MongoContext, ConcreteReflectionActivatorData>().SingleInstance();
//builder.RegisterType<MongoContext>().As<IMongoContext>();
//Registering ApplicationUserManager.
builder.RegisterType<ApplicationUserManager>().As<UserManager<ApplicationUser>>().SingleInstance();
//builder.RegisterType<ApplicationUserManager>().As<ApplicationUserManager>().SingleInstance();
其中两个重要的行是:
builder.RegisterType ApplicationUserManager ().As UserManager ApplicationUser ().SingleInstance();
builder.RegisterType ApplicationUserManager ().As ApplicationUserManager ().SingleInstance;
我的控制器:(我用这 2 个构造函数对其进行了测试,得到了同样的错误)
public AccountController(ApplicationUserManager applicationUserManager)
{
this.applicationUserManager = applicationUserManager;
}
public AccountController(UserManager<ApplicationUser> userManager)
{
this.userManager = userManager;
}
最后是错误:
Autofac.Core.DependencyResolutionException:无法使用可用的服务和参数调用类型为“VotNetMon.ApplicationUserManager”的“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”的构造函数:无法解析参数“Microsoft.AspNetCore.Identity”。 IUserStore
1[VotNetMon.Entities.ApplicationUser] store' of constructor 'Void .ctor(Microsoft.AspNetCore.Identity.IUserStore
1[VotNetMon.Entities.ApplicationUser]、Microsoft.Extensions.Options.IOptions1[Microsoft.AspNetCore.Identity.IdentityOptions], Microsoft.AspNetCore.Identity.IPasswordHasher
1[VotNetMon.Entities.ApplicationUser]、System.Collections.Generic.IEnumerable1[Microsoft.AspNetCore.Identity.IUserValidator
1[VotNetMon.Entities.ApplicationUser]]、System.Collections.Generic.IEnumerable1[Microsoft.AspNetCore.Identity.IPasswordValidator
1[VotNetMon.Entities.ApplicationUser]]、Microsoft.AspNetCore.Identity.ILookupNormalizer、Microsoft.AspNetCore.Identity.IdentityErrorDescriber、System.IServiceProvider、Microsoft.Extensions.Logging.ILogger1[Microsoft.AspNetCore.Identity.UserManager
1[VotNetMon.Entities.ApplicationUser]])'。在 Autofac.Core.Activators.Reflection.ReflectionActivator.GetValidConstructorBindings(IComponentContext 上下文,IEnumerable1 parameters) at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable
1 参数)在 Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 参数)
提前致谢