我試圖在Visual Studio 2015中使用IdentityServer4和ASP.NET Core MVC 1.0.1構建標識服務器。我已成功設置ConfigurationStore
和OperationalStore
使用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?
這是一個錯字。我確定在我的代碼中,AppRole從IdentityRole繼承而不是IdentityUserRole。否則它會給我其他的錯誤。我已更新我的帖子。對於那個很抱歉! –