简体   繁体   中英

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

This is what my project.json file looks like (partial):

{
    "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 . 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:

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 :

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'. There is no implicit reference conversion from 'AppRole' to 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole>'.

wtf?

You are describing the IdentityUserRole twice: for the Role and for the UserRole .

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

Your AppRole should inherit from IdentityRole , not IdentityUserRole :

public class AppRole : IdentityRole...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM