8

我正在使用此示例項目(https://github.com/imranbaloch/ASPNETIdentityWithOnion)作爲我的應用程序體系結構,在此示例中,核心從包括身份框架在內的基礎結構完整地分離出來。從核心域模型解耦ASP.NET身份 - 洋蔥體系結構

在本示例中,作者使用適配器模式來分離核心標識類(IdentityUser,IdentityRole ...)並在核心層中提供類似它們的類。

現在這個示例項目中的問題是,域模型(Product,Images)沒有鏈接到模擬身份的虛擬類(AppUser,ApplicationRole,AppliationUserRoles,...)。

然後,我已經修改了代碼,添加了參考APPUSER

public sealed class Image : BaseEntity 
{ 
    public Image() 
    { 
     Products = new HashSet<Product>(); 
    } 

    public string Path { get; set; } 

    public AppUser AppUser { get; set; } // The Added Reference ... 

    public ICollection<Product> Products { get; set; } 
} 

如果我把「APPUSER」導航屬性「圖像」類中,創建的數據庫將具有比其他四個新表默認的身份框架FIVE表。

Onion Database Problem with Identity Tables

我需要將這些表合併成默認的。 怎麼樣?

編輯:

這是駐留在數據層中的身份模型(我不能從核心參考)。

public class ApplicationIdentityUser : 
    IdentityUser<int, ApplicationIdentityUserLogin, ApplicationIdentityUserRole, ApplicationIdentityUserClaim>, IDomainUser { 

    public ApplicationIdentityUser() 
     : base() { 
     Images = new HashSet<Image>(); 
    } 

    public string Name { get; set; } 
    public virtual ICollection<Image> Images { get; set; } 
} 


public class ApplicationIdentityRole : IdentityRole<int, ApplicationIdentityUserRole> 
{ 
    public ApplicationIdentityRole(){} 

    public ApplicationIdentityRole(string name){Name = name;} 
} 

public class ApplicationIdentityUserRole : IdentityUserRole<int> {} 

public class ApplicationIdentityUserClaim : IdentityUserClaim<int>{} 

public class ApplicationIdentityUserLogin : IdentityUserLogin<int>{} 

而且這是在OnModelCreating方法我的模型構造器:

modelBuilder.Entity<Image>() 
      .Property(e => e.Id) 
      .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 
     modelBuilder.Entity<Image>() 
      .HasMany(e => e.Products) 
      .WithRequired(e => e.Image) 
      .WillCascadeOnDelete(false); 
     modelBuilder.Entity<ApplicationIdentityUser>() 
      .Property(e => e.Id) 
      .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 
     modelBuilder.Entity<ApplicationIdentityRole>() 
      .Property(e => e.Id) 
      .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 
     modelBuilder.Entity<ApplicationIdentityUserClaim>() 
      .Property(e => e.Id) 
      .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 
+1

AspNet *表格是過剩的。需要擺脫他們。檢查你的ApplicationDbContext正在用這些表做什麼? – trailmax 2014-10-27 11:09:47

+0

我的ApplicationDbContext是從IdentityDbContext驅動的,我需要一種方法來存儲用戶信息, 你的意思是我可以使用以Application *開頭的表嗎? – 2014-10-27 13:57:17

+1

我在說應該只有一組*用戶,* UserClaims等表格。看起來你在你的ApplicationDbContext中繼承的方式被破壞了,你不會重寫'Users'和'Roles'等,所以EF爲你創建了兩組表。檢查一下。 – trailmax 2014-10-27 14:11:04

回答

19

好吧,我已經通過以下操作解決了這個:

  1. 包括在你的核心,以微軟的依賴。 AspNet.Identity.Core
  2. 實施IUser接口上的AppUser(此接口來fr om Microsoft.AspNet.Identity.Core)。
  3. 機制IRole接口上的ApplicationRole
  4. 完全擺脫IdentityDbContext並只繼承DbContext
  5. 實現自己的IUserStore *版本提供您的APPUSER
  6. 實現自己的IRoleStore版本提供您的ApplicationRole

我知道,使上Microsoft.AspNet.Identity.Core依賴聽起來很奇怪,但是我們只需要它基本上視爲核心領域模型爲您的應用程序太接口IUSER。

這裏的最終想法是完全取得Microsoft.AspNet.Identity.EntityFramework的完整

感興趣的開發人員可以對此+1,所以我可以上傳一個完整的工作示例在GitHub上。

+2

我剛剛踏上了這段旅程,因爲我得出了與Microsoft.AspNet.Identity.EntityFramework很好的示例代碼相同的結論,但如果您打算用戶成爲第一類域模型,則會干擾您的域。 – 2014-12-05 22:27:37

+2

可以給工作解決方案的鏈接? – user1075940 2015-01-12 21:53:09

+0

真的很想看看你如何擺脫IdentityDbContext,以及如何在Image實體中引用ApplicationIdentityUser,因爲它駐留在Core中而不是數據層中! 任何例子,將不勝感激。 – webStuff 2015-05-29 04:02:47

1

我正在使用這個框架,不需要在每個實體中都有鏈接 要獲取用戶標識引用,我在BaseEntity中添加了一個屬性UserIDBy,因此每個實體都會繼承它。

public abstract class BaseEntity 
{ 
    public int Id { get; set; } 
    public string UserIDBy { get; set; } 
} 

接下來,在web項目中,已經有叫GetUserId(this IIdentity identity)IdentityExtensions.cs,所以股票的UserIDBy在每個創建和編輯操作結果的擴展方法:

創建操作結果:

// POST: /Region/Create 
    [HttpPost] 
    public async Task<ActionResult> Create([Bind(Include = "RegionName")] Region region) 
    { 
     if (ModelState.IsValid) 
     { 
      // TODO: Add insert logic here 
      var id = User.Identity.GetUserId(); 
      region.UserIDBy = id.ToString(); 

      await _regionService.AddAsync(region); 
      return Json(new { success = true }); 
     } 

     return PartialView("_Create", region); 
    } 

編輯操作結果:

//// POST: /Region/Edit/5 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Edit([Bind(Include = "id,RegionName")] Region region) 
    { 
     if (ModelState.IsValid) 
     { 
      var id = User.Identity.GetUserId(); 
      region.UserIDBy = id.ToString(); 
      _regionService.Update(region); 
      return Json(new { success = true }); 
     } 
     return PartialView("_Edit", region); 
    } 

不要忘了將其導入:

using Myapp.Web.Extensions; 
+0

非常聰明。感謝分享。 ASPNETIdentityWithOnion是一個很棒的外殼,但是創建與自定義表格和標識框架的關係令人頭疼不值得。 – JoshYates1980 2017-02-23 21:52:26

+0

你有一個實現ASPNETIdentityWithOnion的開源項目嗎? – JoshYates1980 2017-02-24 01:11:08

0

只是偶然發現了這一點,有同樣的問題。

標記答案的問題是,它仍然引用Microsoft.AspNet,這使得它對未來的.NET Core計劃很粗糙。

主要問題實際上是在覈心中構建身份驗證功能的一般嘗試,這打破了目的。

考慮讓Web認證和授權的內置功能保留在Web層中,並引用表示Core需求的Core用戶對象(UserProfile?)。這也將簡化切換到另一種驗證方法(AD)。

根據您的偏好,您可以從AspNetUser引用Core.UserProfile以避免多次SQL調用,或者只是確保在Core.UserProfile操作上有一個很好的緩存策略。

這允許您控制獨立於Core模型的身份驗證方法。

相關問題