【问题标题】:The type cannot be used as type parameter TRole in the generic type or method UserStore该类型不能用作泛型类型或方法 UserStore 中的类型参数 TRole
【发布时间】:2017-02-12 00:52:30
【问题描述】:

我正在尝试在 Visual Studio 2015 中使用 IdentityServer4 和 ASP.NET Core MVC 1.0.1 构建身份服务器。我已经使用 EntityFramework Core 1.0.1 (@987654322) 成功设置了 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 Identity 作为我在 section 6 中描述的 Identity Server 中的用户存储。默认情况下,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>

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

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 上遇到了构建错误: AppRole 类型不能用作泛型类型或方法“UserStore”中的类型参数“TRole”。没有从“AppRole”到“Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole>”的隐式引用转换。

wtf?

【问题讨论】:

    标签: c# identity


    【解决方案1】:

    您描述了两次IdentityUserRoleRoleUserRole

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

    您的 AppRole 应该继承自 IdentityRole,而不是 IdentityUserRole

    public class AppRole : IdentityRole...
    

    【讨论】:

    • 那是一个错字。我确保在我的代码中 AppRole 继承自 IdentityRole 而不是 IdentityUserRole。否则它会给我其他错误。我已经更新了我的帖子。很抱歉!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    相关资源
    最近更新 更多