2016-07-04 85 views
4

因爲我傾向於Guid作爲我的主鍵類型,我UserRole類實現如下AspNetCore.Identity不使用自定義用戶/角色的實施工作

public class User : IdentityUser<Guid, UserClaim, UserRole, UserLogin> 
{ 
} 

public class Role : IdentityRole<Guid, UserRole, RoleClaim> 
{ 
} 

注意UserClaimUserRoleUserLogin & RoleClaim以同樣的方式被全部實行

這是我DbContext實施

public class ApplicationDbContext : IdentityDbContext<User, Role, Guid, UserClaim, UserRole, UserLogin, RoleClaim, UserToken> 
{ 
} 

目前爲止都很好,除了AspNetCore的新DI容器似乎並不喜歡我的默認自定義實現。下面的行的代碼,來自我的Startup.cs文件會引發下面

services 
    .AddIdentity<User, Role>() 
    .AddEntityFrameworkStores<ApplicationDbContext>() 
    .AddDefaultTokenProviders(); 

GenericArguments [0], 'NewCo.DomainModel.Models.Identity.User',上 「Microsoft.AspNetCore所示的誤差。 Identity.EntityFrameworkCore.UserStore`4 [TUser,TRole,TContext,TKey]' 違反類型'TUser'的約束。

我假設這是因爲默認身份gremlin使用IdentityUser<string>,我使用IdentityUser<Guid>

接下來我該做什麼? (我完全失控的想法)

注:我建立針對微軟的官方.NET的核心和ASP.NET核心發佈,截至週一(2016年6月27日)和Visual Studio更新3的(如果是這樣的任何幫助任何人)

+0

是否有你正在實現你自己的UserClaim,UserRole,UserLogin,RoleClaim和UserToken的版本的原因?我在我自己的應用中使用'Guid'作爲關鍵字,並且與DI沒有任何問題。我實現了我的用戶和角色模型,例如'AppUser:IdentityUser ','AppRole:IdentityRole '。我還沒有發現其他身份模型的自定義實現的需求。 – Brad

+0

@布拉德,我想保持一致 - 儘管如果你的方式有效,那可能是(快樂)妥協。 – series0ne

+0

它在這裏報告爲一個錯誤:https://github.com/aspnet/Identity/issues/829#issuecomment-238804731 – VahidN

回答

5

由於您使用的自定義密鑰類型,你叫AddEntityFrameworkStores時,必須指定它:

services 
    .AddIdentity<User, Role>() 
    .AddEntityFrameworkStores<ApplicationDbContext, Guid>() 
    .AddDefaultTokenProviders(); 
+0

根據文檔完整意義,但我試過了,我仍然得到相同的錯誤。 :-( – series0ne

+0

你應該分享你所有的實體,可能會導致一個通用的參數不兼容。 – Pinpoint

+0

你是否介意把它放到上下文中(可能用代碼示例)?我不完全確定你的意思 – series0ne

0

「UserStore`4 [TUSER,TRole,TContext,TKEY的]'違反了的約束鍵入'TUser'。「

您需要創建使用GUID一個UserStore,以及改變你的DbContext

public class ApplicationUser : IdentityUser<Guid> { } 

public class ApplicationRole : IdentityRole<Guid> { } 

public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, ApplicationDbContext, Guid> 
{ 
    public ApplicationUserStore(ApplicationDbContext context, IdentityErrorDescriber describer = null) : base(context, describer) 
    { 
    } 
} 

新ApplicationDbContext繼承

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid> 
{ 
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } 

    protected override void OnModelCreating(ModelBuilder builder) 
    { 
     base.OnModelCreating(builder); 
    } 
} 

在你startup.cs

services 
    .AddIdentity<ApplicationUser, ApplicationRole>() 
    .AddUserStore<ApplicationUserStore>() 
    .AddEntityFrameworkStores<ApplicationDbContext, Guid>() 
    .AddDefaultTokenProviders(); 
3

我結束了這個解決方案:
其實我做我自己的抽象IdentityDbContext那麼你可以傳遞任何自定義模式,唯一的問題是創建數據庫EF時增加了一個Disriminator場比用戶和角色(繼承)其它所有表

public abstract class ApplicationDbContext<TUser, TRole, TKey, TUserClaim, TUserRole, TUserLogin, TRoleClaim, TUserToken> : IdentityDbContext<TUser, TRole, TKey> 
    where TUser : IdentityUser<TKey> 
    where TRole : IdentityRole<TKey> 
    where TKey : IEquatable<TKey> 
    where TUserClaim : IdentityUserClaim<TKey> 
    where TUserRole : IdentityUserRole<TKey> 
    where TUserLogin : IdentityUserLogin<TKey> 
    where TRoleClaim : IdentityRoleClaim<TKey> 
    where TUserToken : IdentityUserToken<TKey> 
{ 
    public ApplicationDbContext(DbContextOptions options) : base(options) { } 

    protected ApplicationDbContext() { } 

    public new DbSet<TRoleClaim> RoleClaims { get; set; } 
    public new DbSet<TRole> Roles { get; set; } 
    public new DbSet<TUserClaim> UserClaims { get; set; } 
    public new DbSet<TUserLogin> UserLogins { get; set; } 
    public new DbSet<TUserRole> UserRoles { get; set; } 
    public new DbSet<TUser> Users { get; set; } 
    public new DbSet<TUserToken> UserTokens { get; set; } 

    protected override void OnModelCreating(ModelBuilder builder) 
    { 
     base.OnModelCreating(builder); 
    } 
} 

public class AppDbContext : ApplicationDbContext<ApplicationUser, ApplicationRole, Guid, ApplicationUserClaim, ApplicationUserRole, ApplicationUserLogin, ApplicationRoleClaim, ApplicationUserToken> 
{ 
    public AppDbContext(DbContextOptions<AppDbContext> options) 
     : base(options) 
    { 
    } 

    //public new DbSet<ApplicationUserClaim> UserClaims { get; set; } 

    protected override void OnModelCreating(ModelBuilder builder) 
    { 
     base.OnModelCreating(builder); 
     // Customize the ASP.NET Identity model and override the defaults if needed. 
     // For example, you can rename the ASP.NET Identity table names and more. 
     // Add your customizations after calling base.OnModelCreating(builder); 
    } 
} 

    services.AddIdentity<ApplicationUser, ApplicationRole>() 
       .AddEntityFrameworkStores<AppDbContext, Guid>() 
       .AddDefaultTokenProviders() 
       .AddUserStore<UserStore<ApplicationUser, ApplicationRole, AppDbContext, Guid>>() 
       .AddRoleStore<RoleStore<ApplicationRole, AppDbContext, Guid>>() 

public class ApplicationUser : IdentityUser<Guid> 
{ 
    public ApplicationUser() 
    { 
     //this.Id = Guid.NewGuid(); 
    } 

    public ApplicationUser(string userName) : this() { this.UserName = userName; } 
    public Guid ClientId { get; set; } 
    //public new ICollection<ApplicationUserClaim> Claims { get; set; } 
} 

//public class ApplicationRole : IdentityRole<Guid, ApplicationUserRole, ApplicationRoleClaim> 
public class ApplicationRole : IdentityRole<Guid> 
{ 
    public ApplicationRole() 
    { 
     //this.Id = Guid.NewGuid(); 
    } 

    public ApplicationRole(string name) : this() { this.Name = name; } 
} 

public class ApplicationRoleClaim : IdentityRoleClaim<Guid> { } 
//[NotMapped] 
public class ApplicationUserClaim : IdentityUserClaim<Guid> { } 

public class ApplicationUserLogin : IdentityUserLogin<Guid> { } 

public class ApplicationUserRole : IdentityUserRole<Guid> { } 

public class ApplicationUserToken : IdentityUserToken<Guid> { } 
0

指定重點至 。AddEntityFrameworkStore()在啓動時使用自定義用戶和角色entities.while註冊身份的情況下方法

.AddEntityFrameworkStores<IdentityDbContext, TKey>()

,原因是其使用由默認密鑰類型字符串並且將導致誤差在其他鍵類型的情況下。

+1

這不適用於完全自定義標識實現上的自定義鍵類型。請參閱此處接受的答案:https://stackoverflow.com/questions/45127666/aspnetcore-identity-custom-implementation-not-working – series0ne

相關問題