我正在尝试使用现有数据库和表配置身份验证和授权,而不使用Entity Framework(使用Dapper)。
我已经正确配置了Dapper,现在我试图连接SignInManager和UserManager以通过Dapper调用数据库,但是在此之前,我的自定义角色存储遇到了一些错误。
这是当我单击网站上的“注册”按钮时收到的错误(这是一个普通项目,其中包含所有预定义的帐户等内容)
InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNet.Identity.IRoleStore`1[TestAsyncWebsite.Configuration.WrestleStatRole]' while attempting to activate 'Microsoft.AspNet.Identity.RoleManager`1[TestAsyncWebsite.Configuration.WrestleStatRole]'
现在,这是我配置自定义用户,角色,用户存储,角色存储,用户管理器和角色管理器的方式:
public class WrestleStatUser : ApplicationUser
{
public WrestleStatUser() : base()
{
}
}
public class WrestleStatRole : IdentityRole
{
}
public class WrestleStatUserStore : IUserStore<WrestleStatUser>
{
// all methods implemented
}
public class WrestleStatRoleStore : IRoleStore<WrestleStatRole>
{
// all methods implemented
}
public class WrestleStatUserManager : UserManager<WrestleStatUser>
{
public WrestleStatUserManager(IUserStore<WrestleStatUser> store, IOptions<IdentityOptions> optionsAccessor, IPasswordHasher<WrestleStatUser> passwordHasher, IEnumerable<IUserValidator<WrestleStatUser>> userValidators, IEnumerable<IPasswordValidator<WrestleStatUser>> passwordValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, IEnumerable<IUserTokenProvider<WrestleStatUser>> tokenProviders, ILogger<UserManager<WrestleStatUser>> logger, IHttpContextAccessor contextAccessor)
: base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, tokenProviders, logger, contextAccessor)
{
}
}
public class WrestleStatRoleManager : RoleManager<WrestleStatRole>
{
public WrestleStatRoleManager(IRoleStore<WrestleStatRole> store, IEnumerable<IRoleValidator<WrestleStatRole>> roleValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, ILogger<RoleManager<WrestleStatRole>> logger, IHttpContextAccessor contextAccessor) : base(store, roleValidators, keyNormalizer, errors, logger, contextAccessor)
{
}
}
这是我的startup.cs:
services.AddIdentity<WrestleStatUser, WrestleStatRole>()
.AddUserStore<WrestleStatUserStore>()
.AddUserManager<WrestleStatUserManager>()
//.AddRoleStore<RoleStore>()
.AddRoleManager<WrestleStatRoleManager>()
.AddDefaultTokenProviders();
我在这里想念什么?该错误说明了有关RoleManager的信息,我已经定义了自定义RoleManager ...
我看到的一个问题是您的WrestleStatRole继承自IdentityRole,这听起来像Identity的一部分,但实际上是EntityFramework Identity实现的一部分,如果您尝试在没有EF的情况下执行操作,则不应从该继承。
您将需要自己的角色类,并且不应使用EF实现中的任何类。
同样,您在WrestleStatUser中继承的ApplicationUser在Web应用程序项目models文件夹中,但请确保它不从IdentityUser继承,后者是Identity的EntityFramework实现的一部分
要不使用实体框架,必须实现IUserStore和IRoleStore并向di服务注册
services.AddScoped<IUserStore<WrestleStatUser>, UserStore<WrestleStatUser>>();
services.AddScoped<IRoleStore<WrestleStatRole>, RoleStore<WrestleStatRole>>();
并且如上所述,您的用户和角色类不应继承自EF实现,实际上,只要您实现了这些存储并且它们可以工作,它们就根本不需要继承任何东西。
如果实现用户存储和角色存储,则可以使用内置的UserManager,除非您有其他原因,否则不需要自己实现。
如果需要示例代码,可以查看我的cloudscribe项目,我已经实现了一个不使用实体框架的自定义多租户身份实现。实际上,我正在支持可以插入的多个数据层,而EF是其中之一,但它与身份位无关,我根本不使用Microsoft.AspNetCore.Identity.EntityFrameworkCore命名空间中的任何内容。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句