Asp.net-Mvc

如何擴展 User.Identity 的可用屬性

  • February 5, 2015

我正在使用 MVC5 Identity 2.0 讓使用者登錄我的網站,其中身份驗證詳細資訊儲存在 SQL 數據庫中。Asp.net Identity 已以標準方式實現,可在許多線上教程中找到。

IdentityModels 中的 ApplicationUser 類已擴展為包含一些自定義屬性,例如整數 OrganizationId。這個想法是可以創建許多使用者並將其分配給一個公共組織以用於數據庫關係。

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; }
   }

如何從控制器中檢索目前登錄使用者的 OrganizationId 屬性?一旦使用者登錄,這是否可以通過一種方法獲得,或者每次控制器方法執行時,我是否總是根據 UserId 從數據庫中檢索 OrganizationId?

在網上閱讀我看到我需要使用以下內容來獲取登錄的 UserId 等。

using Microsoft.AspNet.Identity;
...
User.Identity.GetUserId();

但是,OrganizationId 不是 User.Identity 中可用的屬性。我是否需要擴展 User.Identity 以包含 OrganizationId 屬性?如果是這樣,我該怎麼做。

我經常需要 OrganizationId 的原因是許多表查詢依賴於 OrganizationId 來檢索與登錄使用者關聯的組織相關的數據。

每當您想使用上述問題的任何其他屬性擴展 User.Identity 的屬性時,首先將這些屬性添加到 ApplicationUser 類,如下所示:

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;
   }

   // Your Extended Properties
   public long? OrganizationId { get; set; }
}

然後你需要像這樣創建一個擴展方法(我在一個新的 Extensions 文件夾中創建我的):

namespace App.Extensions
{
   public static class IdentityExtensions
   {
       public static string GetOrganizationId(this IIdentity identity)
       {
           var claim = ((ClaimsIdentity)identity).FindFirst("OrganizationId");
           // Test for null to avoid issues during local testing
           return (claim != null) ? claim.Value : string.Empty;
       }
   }
}

在 ApplicationUser 類中創建 Identity 時,只需添加 Claim -> OrganizationId ,如下所示:

   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 => this.OrganizationId is a value stored in database against the user
       userIdentity.AddClaim(new Claim("OrganizationId", this.OrganizationId.ToString()));

       return userIdentity;
   }

添加聲明並使用擴展方法後,要將其作為 User.Identity 上的屬性提供,請在要訪問的頁面/文件上添加 using 語句

在我的情況下:using App.Extensions;在控制器中並@using. App.Extensions帶有 .cshtml 視圖文件。

編輯:

您還可以避免在每個視圖中添加 using 語句的方法是轉到 Views 文件夾,並在其中找到 Web.config 文件。現在查找<namespaces>標籤並在那裡添加您的擴展命名空間,如下所示:

<add namespace="App.Extensions" />

保存你的文件,你就完成了。現在每個視圖都會知道你的擴展。

您可以訪問擴展方法:

var orgId = User.Identity.GetOrganizationId();

引用自:https://stackoverflow.com/questions/28335353