[英]Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager` while attempting to activate 'AuthenticateController'
I'm getting this error in Login Controller.我在登录 Controller 中收到此错误。
System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[UsersAuth.IdentityAuth.UserApplication]' while attempting to activate 'UsersAuth.Controllers.AuthenticateController'. System.InvalidOperationException:尝试激活“UsersAuth.Controllers.AuthenticateController”时,无法解析“Microsoft.AspNetCore.Identity.UserManager`1[UsersAuth.IdentityAuth.UserApplication]”类型的服务。
here is Authenticate Controller constructor:这是 Authenticate Controller 构造函数:
public class AuthenticateController : ControllerBase
{
private readonly UserManager<UserApplication> _userManager;
private readonly RoleManager<IdentityRole> _roleManager;
private readonly IConfiguration _configuration;
public AuthenticateController(UserManager<UserApplication> userManager, RoleManager<IdentityRole> roleManager, IConfiguration configuration)
{
_userManager = userManager;
_roleManager = roleManager;
_configuration = configuration;
}
and here is ConfigureServices in startup.cs:这是startup.cs中的ConfigureServices:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddDbContext<ApplicationDbContext>(options=>options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.SaveToken = true;
options.RequireHttpsMetadata = false;
options.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = true,
ValidateAudience = true,
ValidIssuer = Configuration["JWT:ValidIssuer"],
ValidAudience = Configuration["JWT:ValidAudience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JWT:SecretKey"]))
};
});
}
You didn't configure Dependency Injection for UserApplication
.您没有为
UserApplication
配置依赖注入。 There are different ways to do that, for example有不同的方法可以做到这一点,例如
services.AddDefaultIdentity<UserApplication>(options =>
options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
or, if you want more granular control,或者,如果您想要更精细的控制,
services.AddIdentityCore<AppUser>(opt => {
opt.Password.RequiredLength = 8;
...
})
.AddSignInManager()
.AddRoles<IdentityRole>();
I would comment but I don't have rep yet to do it so here it goes: Is UserManager class your creation?我会发表评论,但我还没有代表这样做,所以这里是:UserManager class 是您的创作吗? I don't see a call registering it in IServiceCollection to be resolved when constructing your controller.
在构建您的 controller 时,我没有看到在 IServiceCollection 中注册它的调用要解决。
An example registration for you UserManager could look like this:您的 UserManager 注册示例可能如下所示:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddScoped<UserManager<UserApplication>>();
// other registrations follow
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.