AspNetCore.Identity 不适用于自定义用户/角色实现 [英] AspNetCore.Identity not working with custom User/Role implementation
问题描述
因为我倾向于将 Guid
作为我的主键类型,所以我的 User
和 Role
类实现如下
public class User : IdentityUser{}公共类角色:IdentityRole{}
注意UserClaim
、UserRole
、UserLogin
&RoleClaim
都以相同的方式实现
这是我的 DbContext
实现
public class ApplicationDbContext : IdentityDbContext{}
到目前为止一切都很好,除了 AspNetCore 的新 DI 容器在默认情况下似乎不喜欢我的自定义实现.下面这行代码,来自我的 Startup.cs 文件,抛出如下所示的错误
服务.AddIdentity<用户,角色>().AddEntityFrameworkStores().AddDefaultTokenProviders();
<块引用>
GenericArguments[0], 'NewCo.DomainModel.Models.Identity.User', on'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`4[TUser,TRole,TContext,TKey]'违反了TUser"类型的约束.
我假设这是因为默认的 Identity gremlin 实现为使用 IdentityUser
,其中我使用的是 IdentityUser
.
接下来我该怎么办?(我完全没有想法)
注意:我正在针对截至周一(2016 年 6 月 27 日)的 Microsoft 官方 .NET Core 和 ASP.NET Core 版本以及 Visual Studio Update 3(如果这对任何人有帮助)进行构建
由于您使用的是自定义键类型,因此在调用 AddEntityFrameworkStores
时必须指定它:
服务.AddIdentity<用户,角色>().AddEntityFrameworkStores().AddDefaultTokenProviders();
Because I tend to favour Guid
as my primary key type, my User
and Role
classes are implemented as follows
public class User : IdentityUser<Guid, UserClaim, UserRole, UserLogin>
{
}
public class Role : IdentityRole<Guid, UserRole, RoleClaim>
{
}
Note that UserClaim
, UserRole
, UserLogin
& RoleClaim
are all implemented in the same manner
Here is my DbContext
implementation
public class ApplicationDbContext : IdentityDbContext<User, Role, Guid, UserClaim, UserRole, UserLogin, RoleClaim, UserToken>
{
}
All good so far, except AspNetCore's new DI container doesn't seem to like my custom implementation by default. The following line of code, from my Startup.cs file throws the error shown below
services
.AddIdentity<User, Role>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
GenericArguments[0], 'NewCo.DomainModel.Models.Identity.User', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`4[TUser,TRole,TContext,TKey]' violates the constraint of type 'TUser'.
I am assuming that this is because the default Identity gremlin is implemented to use IdentityUser<string>
, where I'm using IdentityUser<Guid>
.
What do I do next? (I'm all outta ideas)
Note: I'm building against Microsoft's official .NET Core and ASP.NET Core releases as of Monday (June 27th, 2016) and Visual Studio Update 3 (if that's of any help to anyone)
Since you're using a custom key type, you must specify it when calling AddEntityFrameworkStores
:
services
.AddIdentity<User, Role>()
.AddEntityFrameworkStores<ApplicationDbContext, Guid>()
.AddDefaultTokenProviders();
这篇关于AspNetCore.Identity 不适用于自定义用户/角色实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!