【发布时间】:2017-06-11 11:57:48
【问题描述】:
我在登录控制器中收到此错误。
InvalidOperationException:尝试激活“Automobile.Server.Controllers.AuthController”时无法解析“Microsoft.AspNetCore.Identity.UserManager`1[Automobile.Models.Account]”类型的服务。
这里是 Auth Controller 构造函数:
private SignInManager<Automobile.Models.Account> _signManager;
private UserManager<Automobile.Models.Account> _userManager;
public AuthController(UserManager<Models.Account> userManager,
SignInManager<Automobile.Models.Account> signManager)
{
this._userManager = userManager;
this._signManager = signManager;
}
这里是 startup.cs 中的 ConfigureServices:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.Configure<AppConfig>(Configuration.GetSection("AppSettings"));
//var provider = HttpContext.ApplicationServices;
//var someService = provider.GetService(typeof(ISomeService));
services.AddDbContext<Providers.Database.EFProvider.DataContext>(options => options
.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
b => b.MigrationsAssembly("Automobile.Server")
));
services.AddIdentity<IdentityUser, IdentityRole>(options =>
{
options.User.RequireUniqueEmail = false;
})
.AddEntityFrameworkStores<Providers.Database.EFProvider.DataContext>()
.AddDefaultTokenProviders();
//services.AddScoped<SignInManager<Automobile.Models.Account>, SignInManager<Automobile.Models.Account>>();
//services.AddScoped<UserManager<Automobile.Models.Account>, UserManager<Automobile.Models.Account>>();
services.AddMvc();
App.Service = services.BuildServiceProvider();
// Adds a default in-memory implementation of IDistributedCache.
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
// Set a short timeout for easy testing.
options.IdleTimeout = TimeSpan.FromSeconds(10);
options.CookieHttpOnly = true;
});
}
【问题讨论】:
-
在我看来,您将
IdentityUser注册为基本用户类,但随后您使用的是Automobile.Models.Account,当然,ASP.NET Identity 并未在任何地方注册 -
@FedericoDipuma 非常感谢 :) 已解决。
-
你是怎么解决的……?
-
@Lobato in services.AddIdentity 只需将 IdentityUser 替换为您的 Identity User 类
-
@OMID 你为什么不发表你的评论作为答案,这救了我,但在 RnD 的一些严重头痛之后......
标签: c# asp.net asp.net-mvc asp.net-core asp.net-core-identity