ASP.NET Core – 自定义AspNetCore.Identity实现不起作用

我正在构建一个完全自定义的AspNetCore.Identity实现,因为我希望TKey全面成为System.Guid 。 尊敬的我,我已经派出类型……

  • Role : IdentityRole
  • RoleClaim : IdentityRoleClaim
  • User : IdentityUser
  • UserClaim : IdentityUserClaim
  • UserLogin : IdentityUserLogin
  • UserRole : IdentityUserRole
  • UserToken : IdentityUserToken
  • ApplicationDbContext : IdentityDbContext
  • ApplicationRoleManager : RoleManager
  • ApplicationRoleStore : RoleStore
  • ApplicationSignInManager : SignInManager
  • ApplicationUserManager : UserManager
  • **ApplicationUserStore** : UserStore

ApplicationUserStore是问题孩子!

履行

 namespace NewCo.Identity { using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using System; public sealed class Role : IdentityRole { } } namespace NewCo.Identity { using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using System; public sealed class UserRole : IdentityUserRole { } } namespace NewCo.Identity { using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using System; public sealed class RoleClaim : IdentityRoleClaim { } } // The problem is here... namespace NewCo.Identity { using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using System; using System.Security.Claims; public sealed class ApplicationUserStore : UserStore { } } 

错误

类型’NewCo.Identity.Role’不能在generics类型或方法’UserStore’中用作类型参数’TRole’。 没有从’NewCo.Identity.Role’到’Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole>’的隐式引用转换。

据我所知,除非这是一些(co / contra / in)方差问题,否则所有代码都会检查出来……我出错了什么?

您的ApplicationUserStore需要最终的RoleClaim (不要忘记更新相关的NuGet包,否则您不能使用这些新添加的内容):

  ApplicationUserStore : UserStore< User, Role, ApplicationDbContext, Guid, UserClaim, UserRole, UserLogin, UserToken, RoleClaim> 

另外,您的ApplicationRoleStore应该提供如何创建RoleClaim

 protected override RoleClaim CreateRoleClaim(Role role, Claim claim) { return new RoleClaim { RoleId = role.Id, ClaimType = claim.Type, ClaimValue = claim.Value }; } 

此外, ApplicationUserStore也应该提供这些映射:

 protected override UserClaim CreateUserClaim(User user, Claim claim) { var userClaim = new UserClaim { UserId = user.Id }; userClaim.InitializeFromClaim(claim); return userClaim; } protected override UserLogin CreateUserLogin(User user, UserLoginInfo login) { return new UserLogin { UserId = user.Id, ProviderKey = login.ProviderKey, LoginProvider = login.LoginProvider, ProviderDisplayName = login.ProviderDisplayName }; } protected override UserRole CreateUserRole(User user, Role role) { return new UserRole { UserId = user.Id, RoleId = role.Id }; } protected override UserToken CreateUserToken(User user, string loginProvider, string name, string value) { return new UserToken { UserId = user.Id, LoginProvider = loginProvider, Name = name, Value = value }; } 

然后将内置服务重定向到您的自定义服务:

 services.AddScoped, ApplicationUserStore>(); services.AddScoped, ApplicationUserManager>(); services.AddScoped, ApplicationRoleManager>(); services.AddScoped, ApplicationSignInManager>(); services.AddScoped, ApplicationRoleStore>(); services.AddScoped(); services.AddScoped(); 

现在介绍您的自定义服务:

 services.AddIdentity(identityOptions => { // ... }).AddUserStore() .AddUserManager() .AddRoleStore() .AddRoleManager() .AddSignInManager() // You **cannot** use .AddEntityFrameworkStores() when you customize everything //.AddEntityFrameworkStores() .AddDefaultTokenProviders();