【发布时间】:2020-03-11 00:35:14
【问题描述】:
我正在尝试将 .NET Core 的 Identity 包与扩展 IdentityUser<Guid> 的多个类一起使用,但使用单个 UserRole 类。
我有多个为每个用户类型扩展 UserStore<T> 的类和一个扩展 RoleStore<UserRole> 的类。
以下是我的startup.cs:
services.AddIdentity<InternalUser, UserRole>(IdentityOptions)
.AddDefaultTokenProviders()
.AddUserStore<InternalUserStore>()
.AddRoleStore<GenericUserRoleStore>();
services.AddIdentityCore<Contractor>(IdentityOptions)
.AddRoles<UserRole>()
.AddDefaultTokenProviders()
.AddUserStore<ContractorUserStore>()
.AddRoleStore<GenericUserRoleStore>();
services.AddIdentityCore<Homeowner>(IdentityOptions)
.AddRoles<UserRole>()
.AddDefaultTokenProviders()
.AddUserStore<HomeownerUserStore>()
.AddRoleStore<GenericUserRoleStore>();
我的DbContext 没有扩展IdentityDbContext:
public sealed class EntityDbContext: DbContext { }
我遇到了多个错误,因此我将以下内容添加到 DbContext 但我将其注释掉了:
public DbSet<IdentityUserClaim<Guid>> UserClaims { get; set; }
public DbSet<IdentityUserRole<Guid>> UserRoles { get; set; }
我遇到了许多不同的错误:
实例“Dal.IdentityStores.InternalUserStore”上的构建错误 对于 PluginType IUserStore - 以及 PluginType Microsoft.AspNetCore.Identity.RoleManager
1[Models.Entities.Users.UserRole] - and Instance 'Dal.IdentityStores.GenericUserRoleStore' for PluginType Microsoft.AspNetCore.Identity.IRoleStore1[Models.Entities.Users.UserRole] 的实例“RoleManager” - 以及 PluginType 的实例“Dal.IdentityStores.GenericUserRoleStore” Microsoft.AspNetCore.Identity.IRoleStore1[Models.Entities.Users.UserRole] - and Instance 'Dal.IdentityStores.ContractorUserStore' for PluginType Microsoft.AspNetCore.Identity.IUserStore1[Models.Entities.Contractors.Contractor] - 以及 PluginType 的实例“UserClaimsPrincipalFactory” Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory1[Models.Entities.Contractors.Contractor] - and Instance 'UserClaimsPrincipalFactory<Contractor, UserRole>' for PluginType Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory1[Models.Entities.Contractors.Contractor] - 以及 PluginType Microsoft.AspNetCore.Identity.UserManager1[Models.Entities.Homeowners.Homeowner] - and Instance 'UserClaimsPrincipalFactory<Homeowner>' for PluginType Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory1[Models.Entities.Homeowners.Homeowner] 的实例“UserManager”
【问题讨论】:
-
那么,你的问题是什么?
-
我的代码不起作用。我收到上述错误。我该如何修复它以使用多种类型作为“用户”。
-
我已经克隆了你的仓库,它对我有用,所以可能是你的环境问题。
-
@VanoMaisuradze 尝试创建用户并登录。你会看到问题。有一个用于注册/登录的控制器
-
是的,我做到了。谢谢
标签: c# entity-framework asp.net-core .net-core asp.net-identity