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

时间:2016-10-04 00:24:37

标签: c# identity

我正在尝试使用Visual Studio 2015中的IdentityServer4和ASP.NET Core MVC 1.0.1构建一个Identity Server。我已经使用EntityFramework Core成功设置了ConfigurationStoreOperationalStore 1.0.1(section 8)。

这是我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类型不能用作类型参数&#39; TRole&#39;在通用类型或方法&#39; UserStore&#39;。 AppRole&#39;没有隐式参考转换。 to&#39; Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole&gt;&#39;。

跆拳道?

1 个答案:

答案 0 :(得分:0)

您正在描述IdentityUserRole两次:RoleUserRole

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

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

public class AppRole : IdentityRole...
相关问题