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

本文关键字:UserStore 类型参数 TRole 方法 不能 类型 泛型类型 | 更新日期: 2023-09-27 18:11:18

我正在尝试使用IdentityServer4和ASP建立一个身份服务器。. NET Core MVC 1.0.1。我已经使用EntityFramework Core 1.0.1(第8节)成功设置了ConfigurationStoreOperationalStore

这是我的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",
        ......
    }
}

我正在尝试使用ASP。. NET身份作为我在第6节中描述的身份服务器中的用户存储。默认情况下,ASP。. NET Identity使用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>

然后我需要创建自定义用户和角色存储,像这样:

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

最后但并非最不重要的,我的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");
    }
}

但是我在AppUserStore上得到一个构建错误:类型apple不能在泛型类型或方法UserStore中用作类型参数TRole。没有从' apple '到'Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole>'的隐式引用转换。

wtf ?

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

您描述了两次IdentityUserRole: RoleUserRole

public class AppUserRole : IdentityUserRole<int> { }
public class AppRole :     IdentityUserRole<int, AppUserRole, AppRoleClaim> { }

您的批准应该继承IdentityRole,而不是IdentityUserRole:

public class AppRole : IdentityRole...