问题描述
我一直试图在我的身份配置文件中添加自定义字段。该字段在数据库中创建。但是当我尝试访问登录页面时,出现此错误:
错误:
InvalidOperationException:无法解析类型的服务 'Microsoft.AspNetCore.Identity.SignInManager`1 [Microsoft.AspNetCore.Identity.IdentityUser]' 尝试激活时 “ MyProject.Areas.Identity.Pages.Account.LoginModel”。
startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DB_A66C9B_cemmecContext>(optionsBuilder => optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
//ORIGINAL
//services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
// .AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentity<ApplicationUser,IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
// Replace ApplicationUser with your concrete user class
.AddSignInManager<SignInManager<ApplicationUser>>()
.AddDefaultTokenProviders();
//services.AddIdentityCore<IdentityUser>();
services.AddControllersWithViews();
services.AddMvc().AddRazorRuntimeCompilation();
services.AddRazorPages();
}
型号:ApplicationUser
namespace MyProject.Models
{
public class ApplicationUser : IdentityUser
{
public string Nickname { get; set; }
}
}
Applicationdbcontext.cs
namespace MyProject.Data
{
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
}
为什么登录页面失败?
解决方法
这不仅是我要做的一件事。
我找到了SignInManager的所有引用,并发现在许多地方它仍然连接到IdentityUser。然后我意识到我需要再次绑架所有身份物品。
那给了我一些新的错误,但是它们更容易理解我应该怎么做,不记得所有的错误了。但是最重要的是SignInManager<ApplicationUser>
在三个地方都失败了。然后,我必须添加确切的位置:myProject.Models.ApplicationUser
而不是仅ApplicationUser