Asp.net
使用 Asp.Net Identity 2 在 AspNetUserClaims 中儲存使用者資訊有什麼好處?
我想儲存一些額外的使用者資訊。據我了解,以下是通常的選擇:
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; } public string FirstName { get; set; } public string LastName { get; set; } }此處添加了 FirstName 和 LastName,它們將作為附加欄位出現在 AspNetUsers 表中。
但是,現在使用 Asp.Net Identity 似乎還有一個選項可以將此類資訊儲存在 AspNetUserClaims 表中。
誰能給我解釋一下。展望未來,這種資訊可以儲存在 AspNetUserClaims 中。如果是這樣,那麼有沒有人有這方面的任何例子。
在一天結束時,您的登錄使用者將轉換為儲存在 ClaimsIdentity 中的一系列聲明,該聲明在 HttpContext.User.Identity 中代表您的使用者。您可以選擇將 FirstName/LastName 作為列儲存在使用者表中,然後您可以顯式讀出並轉換為適當的聲明(如果需要),或者您可以將它們直接儲存為 AspnetUserClaims 表中的聲明(僅儲存它們作為兩個字元串列),預設情況下會自動添加到使用者的聲明身份中。不過,這兩種方法或多或少是等效的,因此取決於個人喜好。
順便說一句,您希望這些在使用者的 ClaimsIdentity 中的唯一原因是,如果您想保存一個 db 命中只是為了顯示名稱,並始終在 ClaimsIdentity 中使用 FirstName/LastName 聲明。如果您獲取使用者並改用 user.FirstName,則生成名稱聲明也沒有太大價值。