如何擴展User.Identity的可用屬性我正在使用MVC5 Identity 2.0登錄我的網(wǎng)站,其中身份驗證詳細(xì)信息存儲在SQL數(shù)據(jù)庫中。Asp.net Identity已經(jīng)以標(biāo)準(zhǔn)方式實現(xiàn),可以在許多在線教程中找到。IdentityModels中的ApplicationUser類已擴展為包含一些自定義屬性,例如整數(shù)OrganizationId。我們的想法是,可以創(chuàng)建許多用戶并將其分配給公共組織以實現(xiàn)數(shù)據(jù)庫關(guān)系。public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> 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;
}
//Extended Properties
public DateTime? BirthDate { get; set; }
public long? OrganizationId { get; set; }
//Key Mappings
[ForeignKey("OrganizationId")]
public virtual Organization Organization { get; set; }
}如何從控制器中檢索當(dāng)前登錄用戶的OrganizationId屬性?一旦用戶登錄,這是否可通過方法獲得,或者每次執(zhí)行控制器方法時,我是否始終根據(jù)UserId從數(shù)據(jù)庫中檢索OrganizationId?在網(wǎng)上閱讀我看到我需要使用以下內(nèi)容來登錄UserId等。using Microsoft.AspNet.Identity;...User.Identity.GetUserId();但是,OrganizationId不是User.Identity中可用的屬性。我是否需要擴展User.Identity以包含OrganizationId屬性?如果是這樣,我該怎么做呢。我經(jīng)常需要OrganizationId的原因是許多表查詢依賴于OrganizationId來檢索與登錄用戶相關(guān)聯(lián)的與組織相關(guān)的數(shù)據(jù)。
如何擴展User.Identity的可用屬性
慕尼黑8549860
2019-07-23 18:59:53