2017-02-25 11 views
0

支持tenan /公司,我添加/擴展新的屬性到AspUser Table稱爲OrgId在ASP MVC 5,身份2.2角色管理,我加相應ORGID一些其他表,looked here but no answers如何緩存和得到擴展身份,ASPUSER性能在ASP身份2.2

在用戶Login()UserSession

  1. 我怎麼緩存,配置&檢索ORGID,讓我CA n爲組織特定記錄執行DBConext過濾/ CRUD表
  2. 建議:最好將此保存在Claim,FormsToken或Session中 - 如何在會話中設置tenanId上下文?

我知道如何獲得用戶,而不是擴大組織

ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId()); 
+0

是您的ApplicationDbContext類從IdentityDbContext衍生 ??? – Efe

+0

@Efe是的,我相信。我是否必須查詢數據庫,或者是否使用身份對象,因爲我添加了屬性 – transformer

+0

如果您繼承正確,U可以輕鬆地查詢數據庫。我以前做過這個。只要給我30分鐘發佈我爲你做的任何事情。我在去公司的路上 – Efe

回答

1

您定製的用戶類別應該是這樣的:

public class CustomizedUser : IdentityUser 
    { 
     public int OrgId {get; set;} 
     public DateTime JoinDate { get; set; } 
     //... 
     // and other simple fields 

     //Fields related to other tables 
     public virtual ICollection<Article> Articles { get; set; } = new List<Article>(); 
     //...  
} 

而且你CustomizedApplicationDbContext類

public class CustomizedApplicationDbContext : IdentityDbContext<CustomizedUser>, IApplicationDbContext 
{ 
    public CustomizedApplicationDbContext() 
     : base("DefaultConnection", throwIfV1Schema: false) 
    { 
    } 

    public static CustomizedApplicationDbContext Create() 
    { 
     return new CustomizedApplicationDbContext(); 
    } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     //your entity configurations go here if you have any 

     base.OnModelCreating(modelBuilder); 
    } 

    //These fields are neccessary in order to maintain the power of base class 
    public DbChangeTracker Changetracker => base.ChangeTracker; 
    public Database DatabBase => base.Database; 

    //your own dbsets go here except CustomizedUser, because the base class IdentityDbContext<CustomizedUser> handles it 
    public IDbSet<Article> Articles { get; set; } 
    //... 

} 

現在,請記住在代碼中使用CustomizedApplicationDbContext和CustomizedUser替換每個ApplicationDbContext引用(特別是在由mvc創建的ManageController中)。

從現在起,您可以輕鬆地訪問用戶表就像其他表:

var db = new CustomizedApplicationDbContext(); 
var user = db.CustomizedUsers.First(x=> x.OrgId == 10 || Email == "[email protected]"); 

而要獲取當前用戶ORGID,你可以使用這樣的事情(不包括查詢DB):

var currentUserOrgId = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(User.Identity.GetUserId()).OrgId; 

希望這是有幫助的

+0

hi @Efe,如果我有EF Database First,我可以告訴我如何處理這個場景,我應該在哪裏放置像Company或MedicalPlan這樣的新值。或者你也可以推薦,如果我不應該先使用數據庫並更改,或者該怎麼做......我被困在這個循環中 – transformer

+0

@transformer嗨。其實,這取決於你對什麼感覺舒適。對於包含Identiy的方案,我沒有首先使用db,但此鏈接中的第二個和第三個答案可能對您有所幫助。 http://stackoverflow.com/questions/19940014/asp-net-identity-with-ef-database-first-mvc5。現在,如果你問我的推薦,我個人不喜歡像edmx這樣的可視化工具,因爲我幾乎不知道什麼是在幕後進行。此外,asp mvc是一個問題的分離,而不是使用sql查詢或edmx模型創建表,我嘗試着重於mvc的概念並編寫更好的可重用代碼。 – Efe

+0

我個人比較喜歡codefirst,因爲它使我可以完全控制我的域,並且實現更復雜的場景非常簡單。它非常簡單和可管理。如果您的項目進度低於50%,我建議您切換到codefirst。再次,這取決於你。有很多關於ef codefirst在互聯網上的優缺點的文章,請在制定或更改您的決定前檢查它們,但我敢打賭,如果您正在尋找靈活性,您將選擇codefirst。 – Efe

1

你可以在ASP.NET Identity當前用戶,如下圖所示:

ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext() 
    .GetUserManager<ApplicationUserManager>() 
    .FindById(System.Web.HttpContext.Current.User.Identity.GetUserId()); 

//If you use int instead of string for primary key, use this: 
ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext() 
    .GetUserManager<ApplicationUserManager>() 
    .FindById(Convert.ToInt32(System.Web.HttpContext.Current.User.Identity.GetUserId())); 


對於來自AspNetUsers表中獲取自定義屬性:

ApplicationUser user = UserManager.FindByName(userName); 
string name = user.Name; 

希望這有助於...

+0

嗨,我這樣做,並每次去數據庫獲取信息,就像我的問題1)有沒有辦法來緩存該信息?或者在下面的權利要求2)中加入它會更好?efe也提供了一些很好的建議,但我的情況是數據庫第一,我可以先用DB輕鬆做到這一點。 – transformer

+0

1)你說得對,但我不確定哪一個更好。但通常這種方法似乎是在我第一次搜索這個問題時使用的。另一方面,你可能會看看[有效的身份相關實體緩存](http://stackoverflow.com/questions/22166063/efficient-caching-of-identity-related-entities)和[獲取ASP.NET身份當前用戶在視圖](http://stackoverflow.com/questions/27139068/get-asp-net-identity-current-user-in-view)線程。 2)我已經在DB First中多次使用了給定的方法,沒有任何問題。 –