簡體   English   中英

使用自定義身份用戶(ASP.NET 5.2EF 6和身份2)時,用戶角色表上的額外列

[英]Extra column on user-roles table when using custom identity user(ASP.NET 5.2EF 6 and Identity 2)

我的MVC應用程序使用的是基於4層的體系結構:Domain,Data,Service和MVC。 在我的域層中,我創建了一個從IdentityUser繼承的Employee類。 它包含自定義屬性和我從MVC的identitymodes中的應用程序用戶類獲得的GenerateUserIdentityAsync方法。

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繼承的上下文。 我在數據庫集中沒有提到員工。 關於我的應用程序,尤其是員工類,我也添加了許多內容。

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列,如下所示:

select * from aspnetuserroles中的屏幕快照 UserId和RoleId是表的PK,而Employee_Id是Employee中的FK。 我認為UserId應該改為Fk。 這確實是個問題,因為例如Usermanager的Authorizate批注和GetRoles效果不佳。 UserManager.GetRoles(Employee.Id)始終返回null,而我得到的Employee.Id具有我從數據庫中獲得的正確值。 我認為這是因為比較是在employee_id而不是userid上進行的。 您對此有想法嗎?我是ASP.NET的初學者。 先感謝您。

通過從DbContext:IdentityDbContext中刪除了genericity(Employee)進行了更正,現在我的上下文是:公共類MyCWCContexte:IdentityDbContext

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM