ASP.NET 中没有 Entity Framework 的身份验证和授权 5 MVC 6
Authentication and Authorization without Entity Framework in ASP.NET 5 MVC 6
我正在尝试使用我现有的数据库和表来配置我的身份验证和授权,而不是使用 Entity Framework(使用 Dapper)。
我已经正确配置了 Dapper,现在我正在尝试连接 SignInManager 和 UserManager 以通过 Dapper 调用我的数据库,但在此之前,我 运行 进入了一些我的自定义角色存储出错。
这是我在网站上单击 "Register" 按钮时收到的错误消息(这只是一个普通项目,所有预定义帐户等都开箱即用)
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 的情况下做事,你应该不继承。
您需要自己的角色 class 并且不应使用来自 EF 实现的任何 classes。
同样,您在 WrestleStatUser 中继承的 ApplicationUser 位于 Web 应用程序项目模型文件夹中,但请确保它不继承自 IdentityUser,后者是 EntityFramework 身份实现的一部分
要不使用 Entity Framework,您必须实施 IUserStore 和 IRoleStore 并将它们注册到 di 服务
services.AddScoped<IUserStore<WrestleStatUser>, UserStore<WrestleStatUser>>();
services.AddScoped<IRoleStore<WrestleStatRole>, RoleStore<WrestleStatRole>>();
并且如前所述,您的用户和角色 classes 不应该继承自 EF 实现,事实上,只要您实现了这些商店并且它们可以工作,它们根本不需要继承任何东西。
如果您实现了用户存储和角色存储,则可以使用内置的 UserManager,除非您有其他原因,否则不需要自己实现。
如果您需要示例代码,可以查看我的 cloudscribe project 我已经实现了一个不使用 entity framework 的自定义多租户身份实现。实际上,我支持可以插入的多个数据层,EF 就是其中之一,但它与身份位无关,我根本没有使用 Microsoft.AspNetCore.Identity.EntityFrameworkCore 命名空间中的任何内容。
我正在尝试使用我现有的数据库和表来配置我的身份验证和授权,而不是使用 Entity Framework(使用 Dapper)。
我已经正确配置了 Dapper,现在我正在尝试连接 SignInManager 和 UserManager 以通过 Dapper 调用我的数据库,但在此之前,我 运行 进入了一些我的自定义角色存储出错。
这是我在网站上单击 "Register" 按钮时收到的错误消息(这只是一个普通项目,所有预定义帐户等都开箱即用)
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 的情况下做事,你应该不继承。
您需要自己的角色 class 并且不应使用来自 EF 实现的任何 classes。
同样,您在 WrestleStatUser 中继承的 ApplicationUser 位于 Web 应用程序项目模型文件夹中,但请确保它不继承自 IdentityUser,后者是 EntityFramework 身份实现的一部分
要不使用 Entity Framework,您必须实施 IUserStore 和 IRoleStore 并将它们注册到 di 服务
services.AddScoped<IUserStore<WrestleStatUser>, UserStore<WrestleStatUser>>();
services.AddScoped<IRoleStore<WrestleStatRole>, RoleStore<WrestleStatRole>>();
并且如前所述,您的用户和角色 classes 不应该继承自 EF 实现,事实上,只要您实现了这些商店并且它们可以工作,它们根本不需要继承任何东西。
如果您实现了用户存储和角色存储,则可以使用内置的 UserManager,除非您有其他原因,否则不需要自己实现。
如果您需要示例代码,可以查看我的 cloudscribe project 我已经实现了一个不使用 entity framework 的自定义多租户身份实现。实际上,我支持可以插入的多个数据层,EF 就是其中之一,但它与身份位无关,我根本没有使用 Microsoft.AspNetCore.Identity.EntityFrameworkCore 命名空间中的任何内容。