gan*_*ers 16 c# asp.net asp.net-mvc dapper asp.net-core
我正在尝试使用现有的数据库和表来配置我的身份验证和授权,而不使用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]'
Run Code Online (Sandbox Code Playgroud)
目前,这是我如何配置我的自定义用户,角色,用户界面,角色存储,用户管理器和角色管理器:
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)
{
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的startup.cs:
services.AddIdentity<WrestleStatUser, WrestleStatRole>()
.AddUserStore<WrestleStatUserStore>()
.AddUserManager<WrestleStatUserManager>()
//.AddRoleStore<RoleStore>()
.AddRoleManager<WrestleStatRoleManager>()
.AddDefaultTokenProviders();
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?错误说明了RoleManager,我已经定义了我的自定义RoleManager ......
Joe*_*tte 14
我看到的一个问题是你的WrestleStatRole继承自IdentityRole,它可能听起来像Identity的一部分,但它确实是EntityFramework Identity实现的一部分,如果你试图在没有EF的情况下做事,你就不应该继承它.
您需要自己的角色类,不应使用EF实现中的任何类.
类似地,您在WrestleStatUser中继承的ApplicationUser位于Web应用程序项目模型文件夹中,但请确保它不从IdentityUser继承,IdentityUser是EntityFramework身份实现的一部分
要不使用Entity Framework,您必须实现IUserStore和IRoleStore并使用di服务注册它们
services.AddScoped<IUserStore<WrestleStatUser>, UserStore<WrestleStatUser>>();
services.AddScoped<IRoleStore<WrestleStatRole>, RoleStore<WrestleStatRole>>();
Run Code Online (Sandbox Code Playgroud)
并且如上所述,您的用户和角色类不应该继承自EF实现,事实上,只要您实现了这些商店并且它们有效,它们就不需要继承任何东西.
如果您实现了userstore和rolestore,则可以使用内置的UserManager,除非您有其他原因,否则不需要自己实现.
如果您需要示例代码,您可以查看我的cloudscribe项目,我实现了一个不使用实体框架的自定义多租户身份实现.实际上我支持可以插入的多个数据层,而EF是其中之一,但它远离身份位,我根本没有使用任何来自Microsoft.AspNetCore.Identity.EntityFrameworkCore命名空间的东西.
归档时间: |
|
查看次数: |
8393 次 |
最近记录: |