AspNetCore.Identity不适用于自定义用户/角色实现 [英] AspNetCore.Identity not working with custom User/Role implementation

查看:969
本文介绍了AspNetCore.Identity不适用于自定义用户/角色实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我倾向于将 Guid 作为主键类型,我的用户角色类实现如下

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>
{
}

请注意, UserClaim UserRole UserLogin & RoleClaim 都以相同的方式实现

Note that UserClaim, UserRole, UserLogin & RoleClaim are all implemented in the same manner

这是我的 DbContext 实现

public class ApplicationDbContext : IdentityDbContext<User, Role, Guid, UserClaim, UserRole, UserLogin, RoleClaim, UserToken>
{
}

除了AspNetCore的新DI容器,默认情况下,我似乎喜欢我的自定义实现。以下代码行来自我的Startup.cs文件,会抛出以下错误:

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。用户'$ on
'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`4 [TUser,TRole,TContext,TKey]'
违反类型'TUser'的约束。

GenericArguments[0], 'NewCo.DomainModel.Models.Identity.User', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`4[TUser,TRole,TContext,TKey]' violates the constraint of type 'TUser'.

我假设这是因为默认的Identity gremlin被实现为使用 IdentityUser< string> ,其中我正在使用 IdentityUser&Guid。>

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)

注意:我正在构建Microsoft的官方.NET Core和ASP.NET Core

推荐答案

发布于2016年6月27日星期一(6月27日)和Visual Studio Update 3(如果对任何人有任何帮助)

由于您使用自定义键类型,因此您必须在调用 AddEntityFrameworkStores 时指定:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆