[英]The type cannot be used as type parameter TRole in the generic type or method UserStore
I am trying to build an Identity Server using IdentityServer4 and ASP.NET Core MVC 1.0.1 in Visual Studio 2015. I already successfully setup the ConfigurationStore
and OperationalStore
using EntityFramework Core 1.0.1 ( section 8 ). 我正在尝试在Visual Studio 2015中使用IdentityServer4和ASP.NET Core MVC 1.0.1构建Identity Server。我已经使用EntityFramework Core 1.0.1( 第8节 )成功设置了
ConfigurationStore
和OperationalStore
。
This is what my project.json
file looks like (partial): 这是我的
project.json
文件的样子(部分):
{
"dependencies": {
"Microsoft.NETCore.App" {
"version": "1.0.1",
"type": "platform"
},
"Microsoft.AspNetCore.Mvc": "1.0.1",
/////// Entity Framework /////////
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.1",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
/////// ASP.NET Identity /////////
"Microsoft.AspNetCore.Identity": "1.0.0",
"Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
/////// Identity Server //////////
"IdentityServer4": "1.0.0-rc1-update2",
"IdentityServer4.AspNetIdentity": "1.0.0-rc1-update2",
"IdentityServer4.EntityFramework": "1.0.0-rc1-update2",
......
}
}
I am trying to use ASP.NET Identity as my user store in the Identity Server described in section 6 . 我试图将ASP.NET Identity用作第6节中所述的Identity Server中的用户存储。 By default the ASP.NET Identity uses
string
as the key for all the tables but I want to use int
so I changed them like this: 默认情况下,ASP.NET身份使用
string
作为所有表的键,但是我想使用int
所以我像这样更改它们:
public class AppUserToken : IdentityUserToken<int> { }
public class AppUserLogin : IdentityUserLogin<int> { }
public class AppUserClaim : IdentityUserClaim<int> { }
public class AppUserRole : IdentityUserRole<int> { }
public class AppRoleClaim : IdentityRoleClaim<int> { }
public class AppRole : IdentityRole<int, AppUserRole, AppRoleClaim> { }
public class AppUser : IdentityUser<int, AppUserClaim, AppUserRole, AppUserLogin>
And then I need to create custom user and role store like this: 然后,我需要像这样创建自定义用户和角色存储:
public class AppRoleStore : RoleStore<AppRole, AppIdentityDbContext, int, AppUserRole, AppRoleClaim>
{
public AppRoleStore(AppIdentityDbContext context, IdentityErrorDescriber describer = null)
: base(context, describer)
{}
.......
}
public class AppUserStore : UserStore<AppUser, AppRole, AppIdentityDbContext, int, AppUserClaim, AppUserRole, AppUserLogin, AppUserToken>
{
public AppUserStore(AppIdentityDbContext context, IdentityErrorDescriber describer = null)
: base(context, describer)
{ }
............
}
Last but not least, my AppIdentityDbContext
: 最后但并非最不重要的是,我的
AppIdentityDbContext
:
public class AppIdentityDbContext : IdentityDbContext<AppUser, AppRole, int, AppUserClaim, AppUserRole, AppUserLogin, AppRoleClaim, AppUserToken>
{
public AppIdentityDbContext(DbContextOptions<AppIdentityDbContext> options)
: base(options)
{ }
public override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<AppUser>().ToTable("Users");
builder.Entity<AppUserLogin>().ToTable("UserLogins");
builder.Entity<AppUserClaim>().ToTable("UserClaims");
builder.Entity<AppUserToken>().ToTable("UserTokens");
builder.Entity<AppRole>().ToTable("Roles");
builder.Entity<AppRoleClaim>().ToTable("RoleClaims");
builder.Entity<AppUserRole>().ToTable("UserRoles");
}
}
But I am getting a build error on AppUserStore
: The type AppRole cannot be used as type parameter 'TRole' in the generic type or method 'UserStore'. 但是我在
AppUserStore
上AppUserStore
构建错误: AppUserStore
类型不能用作通用类型或方法“ UserStore”中的类型参数“ TRole”。 There is no implicit reference conversion from 'AppRole' to 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole>'. 没有从“ AppRole”到“ Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole>”的隐式引用转换。
wtf? wtf?
You are describing the IdentityUserRole
twice: for the Role
and for the UserRole
. 您正在两次描述
IdentityUserRole
:对于Role
和对于UserRole
。
public class AppUserRole : IdentityUserRole<int> { }
public class AppRole : IdentityUserRole<int, AppUserRole, AppRoleClaim> { }
Your AppRole should inherit from IdentityRole
, not IdentityUserRole
: 您的AppRole应该继承自
IdentityRole
,而不是IdentityUserRole
:
public class AppRole : IdentityRole...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.