2016-11-09 28 views
0

我爲我的MVC應用程序使用基於4層的體系結構:域,數據,服務和MVC。 在我的領域層,我創建了一個從IdentityUser繼承的Employee類。它包含自定義屬性和我從MVC中的identitymodes中的應用程序用戶類獲得的GenerateUserIdentityAsync方法。使用自定義標識用戶(ASP.NET 5.2EF 6和Identity 2)時用戶角色表上的額外列

public class Employee : IdentityUser 
{ 
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<Employee> manager) 
    { 
     // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType 
     var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); 
     // Add custom user claims here 
     return userIdentity; 
    } 
    public String CIN { get; set; } 
    public String FirstName { get; set; } 

在我的數據層,我創建了一個從IdentityDbContext繼承的上下文。我沒有在我的dbsets中提到員工。我還增加了許多關於我的應用程序,特別是員工班。

public class MyCWCContexte : IdentityDbContext<Employee> 
{ 
    public MyCWCContexte() 
     : base("name=CWCDB") 
    { 

    } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     base.OnModelCreating(modelBuilder); 

最後改變我所做的是,我已經改變了StartUp.Auth.cs和IdentityConfig文件,使他們符合我的Employee類和我的背景。

public class ApplicationUserManager : UserManager<CWC.Domain.Entities.Employee> 
{ 
    public ApplicationUserManager(IUserStore<CWC.Domain.Entities.Employee> store) 
     : base(store) 
    { 
    } 

    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
    { 
     var manager = new ApplicationUserManager(new UserStore<CWC.Domain.Entities.Employee>(context.Get<MyCWCContexte>())); 
     // Configure validation logic for usernames 
     manager.UserValidator = new UserValidator<CWC.Domain.Entities.Employee>(manager) 
     { 
      AllowOnlyAlphanumericUserNames = false, 
      RequireUniqueEmail = true 
     }; 

從遷移生成數據庫進展順利,問題出在aspnet自動生成的表,例如IdentityUserRole類。當試圖添加一個用戶,並添加一個角色,我已經看到了aspnetuserroles包含3列如下陳述:

Screenshot of select * from aspnetuserroles 用戶ID和角色ID是表的的PK,和EMPLOYEE_ID是從僱員FK。我認爲UserId應該是Fk。 這真的是一個問題,因爲Authorizate註釋和例如usermanager的GetRoles不能很好地工作。 UserManager.GetRoles(Employee.Id)總是返回null,並且我得到的Employee.Id具有從數據庫獲得的正確值。 我認爲這是因爲比較是在employee_id而不是userid上進行的。 你對此有個想法嗎?我是ASP.NET的初學者。先謝謝你。

+0

你創建一個角色類自己也? –

回答

0

與的DbContext取出泛型(員工)更正它:IdentityDbContext 我的背景是現在: 公共類MyCWCContexte:IdentityDbContext

+0

這是問題的答案還是附加問題? –

+0

@GertArnold一個答案 – Amstelsus

相關問題