[英]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.