【发布时间】:2017-07-16 10:47:48
【问题描述】:
我正在构建一个完全自定义的 AspNetCore.Identity 实现,因为我希望 TKey 全面成为 System.Guid。恕我直言,我已经为...派生了类型。
Role : IdentityRole<Guid, UserRole, RoleClaim>RoleClaim : IdentityRoleClaim<Guid>User : IdentityUser<Guid, UserClaim, UserRole, UserLogin>UserClaim : IdentityUserClaim<Guid>UserLogin : IdentityUserLogin<Guid>UserRole : IdentityUserRole<Guid>-
UserToken : IdentityUserToken<Guid>
-
ApplicationDbContext : IdentityDbContext<User, Role, Guid, UserClaim, UserRole, UserLogin, RoleClaim, UserToken>
ApplicationRoleManager : RoleManager<Role>ApplicationRoleStore : RoleStore<Role, ApplicationDbContext, Guid, UserRole, RoleClaim>ApplicationSignInManager : SignInManager<User>ApplicationUserManager : UserManager<User>**ApplicationUserStore** : UserStore<User, Role, ApplicationDbContext, Guid, UserClaim, UserRole, UserLogin, UserToken>
ApplicationUserStore 是问题孩子!
实施
namespace NewCo.Identity
{
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using System;
public sealed class Role : IdentityRole<Guid, UserRole, RoleClaim>
{
}
}
namespace NewCo.Identity
{
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using System;
public sealed class UserRole : IdentityUserRole<Guid>
{
}
}
namespace NewCo.Identity
{
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using System;
public sealed class RoleClaim : IdentityRoleClaim<Guid>
{
}
}
// 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<User, Role, ApplicationDbContext, Guid, UserClaim, UserRole, UserLogin, UserToken>
{
}
}
错误
类型“NewCo.Identity.Role”不能用作类型参数 泛型类型或方法“UserStore”中的“TRole”。那里 没有从 'NewCo.Identity.Role' 到的隐式引用转换 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole>'。
据我所知,除非这是一些(co/contra/in)方差问题,否则所有代码都会检查出来......我做错了什么?
【问题讨论】:
-
好问题!约束是
where TRole : IdentityRole<TKey, TUserRole, IdentityRoleClaim<TKey>>。由于这是 class 约束,因此没有协变/逆变。 IMO 他们只是忘记添加TRoleClaim通用参数:( -
@IvanStoev 谢谢,我已经向 ASPNETCore/Identity 团队提出了这个问题。
标签: c# entity-framework asp.net-identity