简体   繁体   English

该类型不能用作泛型类型或方法UserStore中的类型参数TRole

[英]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节 )成功设置了ConfigurationStoreOperationalStore

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'. 但是我在AppUserStoreAppUserStore构建错误: 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.

相关问题 通用方法-类型不能用作类型参数 - Generic method - Type cannot be used as Type Parameter 该类型不能用作通用类型或方法中的类型参数 - The type cannot be used as type parameter in the generic type or method 类型&#39;&#39;不能在通用类型或方法中用作类型参数&#39;T&#39; - The type '' cannot be used as type parameter 'T' in the generic type or method 类型“ XXX”不能用作通用类型或方法中的类型参数“ T” - The type 'XXX' cannot be used as type parameter 'T' in the generic type or method 类型“ T”不能用作通用类型或方法中的类型参数“ T” - The type 'T' cannot be used as type parameter 'T' in the generic type or method 存储库不能用作方法的通用类型中的类型参数 - Repository cannot be used as a type parameter in the generic type of method 不能在泛型类型或方法中用作类型参数“TElement” - Cannot be used as type parameter 'TElement' in the generic type or method 当类型已知时,类型&#39;T&#39;不能用作泛型类型或方法错误中的类型参数 - The type 'T' cannot be used as type parameter in the generic type or method error when type is known 类型“”不能用作泛型类型“ICrudAppService”中的类型参数“TEntityDto” - The type '' cannot be used as type parameter 'TEntityDto' in the generic type 'ICrudAppService' Xamarin / Realm-该类型不能用作通用类型或方法中的类型参数“ T” - Xamarin/Realm - The type cannot be used as type parameter 'T' in the generic type or method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM