2016-10-04 80 views
0

我试图在Visual Studio 2015中使用IdentityServer4和ASP.NET Core MVC 1.0.1构建标识服务器。我已成功设置ConfigurationStoreOperationalStore使用EntityFramework核心1.0.1(section 8)。该类型不能用作通用类型或方法中的类型参数TRole UserStore

这是我的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身份为section 6描述的身份服务器在我的用户存储。默认情况下,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"); 
    } 
} 

但我得到的生成错误: 类型AppRole不能用作泛型类型或方法'UserStore'中的类型参数'TRole'。没有从'AppRole'到'Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole>'的隐式引用转换。

wtf?

回答

0

您描述的是IdentityUserRole两次:RoleUserRole

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

你AppRole应该IdentityRole继承,而不是IdentityUserRole

public class AppRole : IdentityRole... 
+0

这是一个错字。我确定在我的代码中,AppRole从IdentityRole继承而不是IdentityUserRole。否则它会给我其他的错误。我已更新我的帖子。对于那个很抱歉! –

相关问题