[英]How to add custom TUser and TRole into services.AddIdentity in ASP.NET Core?
[英]Cant add AddIdentity service in NET Core web api
我正在使用 NET Core 3.1 + EntityFramework + PSQL。 尝试遵循教程和 msdn,但我的代码不起作用。
这是我的startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
//params
};
});
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<MyContext>()
}
这是我的MyContext.cs
public class MyContext : IdentityDbContext<ApplicationUser>
{
public DbSet<Flowers> Flowers { get; set; }
public DbSet<Post> Posts { get; set; }
public MyContext() : base()
{
Database.EnsureCreated();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql("Host=localhost...");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
这是我的ApplicationUser.cs
public class ApplicationUser : IdentityUser
{
public string SpecialNumber{ get; set; }
public ICollection<Flower> Flowers { get; set; }
}
当我尝试运行应用程序时,出现异常System.AggregateException: '某些服务无法构建'
某些服务无法构建(验证服务描述符时出错:“ServiceType: Microsoft.AspNetCore.Identity.ISecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.SecurityStampValidator
1[FlowersApp.Models.ApplicationUser]': Unable to resolve service for type 'FlowersApp.Models.MyContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore
9[FlowersApp.Models.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,FlowersApp.Models.MyContext,System.String”时,1[FlowersApp.Models.ApplicationUser]': Unable to resolve service for type 'FlowersApp.Models.MyContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore
Microsoft.AspNetCore.Identity.IdentityUserClaim1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken
1 [System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim1[System.String]]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.ITwoFactorSecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator
1[System.String]]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.ITwoFactorSecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator
1[FlowersApp.Models.ApplicationUser]”:尝试激活“Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore”时无法解析“FlowersApp.Models.MyContext”类型的服务9[FlowersApp.Models.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,FlowersApp.Models.MyContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim
1[System.String]]'.) (验证服务描述符时出错“ServiceType: Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory1[FlowersApp.Models.ApplicationUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory
1[FlowersApp.Models.ApplicationUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory
2[FlowersApp.Models.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole]':尝试激活'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore9[FlowersApp.Models.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,FlowersApp.Models.MyContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
'时无法解析类型'FlowersApp.Models.MyContext'的服务9[FlowersApp.Models.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,FlowersApp.Models.MyContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim
1[System.String]]'。)(验证时出错服务描述符“ServiceType:Microsoft.AspNetCore.Identity.UserManager1[FlowersApp.Models.ApplicationUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserManager
1[FlowersApp.Models.ApplicationUser]”:无法解析“FlowersApp”类型的服务. Models.MyContext' 同时尝试激活'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore9[FlowersApp.Models.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,FlowersApp.Models.MyContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim
1[System.String]]'。)(验证服务描述符时出错“ServiceType: Microsoft.AspNetCore.Identity.SignInManager1[FlowersApp.Models.ApplicationUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.SignInManager
1[FlowersApp.Models.ApplicationUser]”:尝试激活“Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore9[FlowersApp.Models.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,FlowersApp.Models.MyContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
”时无法解析“FlowersApp.Models.MyContext”类型的服务9[FlowersApp.Models.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,FlowersApp.Models.MyContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim
1[System.String]]'。)(验证服务描述符时出错“ServiceType: Microsoft.AspNetCore.Identity.RoleManager1[Microsoft.AspNetCore.Identity.IdentityRole] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.RoleManager
1[Microsoft.AspNetCore.Identity.IdentityRole]”:无法解析服务类型'FlowersApp.Models.MyContext' 同时尝试激活 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.RoleStore5[Microsoft.AspNetCore.Identity.IdentityRole,FlowersApp.Models.MyContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserRole
1[系统.Str ing],Microsoft.AspNetCore.Identity.IdentityRoleClaim1[System.String]]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.IUserStore
1[FlowersApp.Models.ApplicationUser] Lifetime:Scoped ImplementationType: Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore9[FlowersApp.Models.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,FlowersApp.Models.MyContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
1[System.String],Microsoft .AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim
1[ System.String]]':尝试激活“Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore9[FlowersApp.Models.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,FlowersApp.Models.MyContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
9[FlowersApp.Models.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,FlowersApp.Models.MyContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin
1[System.String] ],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim
1[System.String]]'。)(验证服务描述符“ServiceType: Microsoft.AspNetCore.Identity.IRoleStore”时出错1[Microsoft.AspNetCore.Identity.IdentityRole] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.EntityFrameworkCore.RoleStore
5[Microsoft.AspNetCore.Identity.IdentityRole,FlowersApp.Models.MyContext,System.String,Microsoft.AspNetCore.Identity。 IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim
1[System.String]]”:尝试激活“Microsoft.AspNetCore.Identity.EntityFrameworkCore”时无法解析“FlowersApp.Models.MyContext”类型的服务。 RoleStore5[Microsoft.AspNetCore.Identity.IdentityRole,FlowersApp.Models.MyContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserRole
5[Microsoft.AspNetCore.Identity.IdentityRole,FlowersApp.Models.MyContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserRole
1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim`1[System.String]]'。)
找不到这个错误的原因。 我的课程继承正确吗?
当我从Startup.cs 中删除这部分代码时应用程序运行
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<MyContext>()
将此添加到您的ConfigureServices
的顶部您需要告诉框架它如何创建MyContext
的实例(注入该服务)
services.AddDbContext<MyContext>(options => /*<<<ADD YOUR DBCONTEXT CONFIGURATION >>>*/);
您忘记在ConfigureServices
部分中写下这一行
services.AddDbContext<MyContext>(options =>
options.UseNpgsql("your connection string"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.