【发布时间】:2019-01-08 17:18:11
【问题描述】:
我在尝试将我的 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 ()。作为ApplicationUserManager().SingleInstance;
我的控制器:(我用这 2 个构造函数对其进行了测试,得到了同样的错误)
public AccountController(ApplicationUserManager applicationUserManager)
{
this.applicationUserManager = applicationUserManager;
}
public AccountController(UserManager<ApplicationUser> userManager)
{
this.userManager = userManager;
}
最后是错误:
Autofac.Core.DependencyResolutionException: 没有构造函数 发现与 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' 类型 'VotNetMon.ApplicationUserManager' 可以用可用的调用 服务和参数:无法解析参数 'Microsoft.AspNetCore.Identity.IUserStore
1[VotNetMon.Entities.ApplicationUser] store' of constructor 'Void .ctor(Microsoft.AspNetCore.Identity.IUserStore1[VotNetMon.Entities.ApplicationUser], Microsoft.Extensions.Options.IOptions1[Microsoft.AspNetCore.Identity.IdentityOptions], Microsoft.AspNetCore.Identity.IPasswordHasher1[VotNetMon.Entities.ApplicationUser], System.Collections.Generic.IEnumerable1[Microsoft.AspNetCore.Identity.IUserValidator1[VotNetMon.Entities.ApplicationUser]], System.Collections.Generic.IEnumerable1[Microsoft.AspNetCore.Identity.IPasswordValidator1[VotNetMon.Entities.ApplicationUser]], Microsoft.AspNetCore.Identity.ILookupNormalizer, Microsoft.AspNetCore.Identity.IdentityErrorDescriber, System.IServiceProvider, Microsoft.Extensions.Logging.ILogger1[Microsoft.AspNetCore.Identity.UserManager1[VotNetMon.Entities.ApplicationUser]])'。 在 Autofac.Core.Activators.Reflection.ReflectionActivator.GetValidConstructorBindings(IComponentContext 上下文,IEnumerable1 parameters) at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable1 参数)在 Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 参数)
提前致谢
【问题讨论】:
标签: asp.net asp.net-core code-injection usermanager