【asp.net core 系列】15 自定義Identity
阿新 • • 發佈:2020-07-09
# 0. 前言
在之前的文章中簡單介紹了一下asp.net core中的Identity,這篇文章將繼續針對Identity進行進一步的展開。
# 1. 給Identity新增額外的資訊
在《【asp.net core 系列】13 Identity 身份驗證入門》一文中,我們大概瞭解瞭如何使用Identity,以及如何儲存一些資訊以便後續的驗證。這裡我們將深入討論一下如何給Identity新增更多的資訊。
我們知道在給Identity新增資料的時候,需要新增一個Claim物件。我們先回顧一下Claim的資訊,Claim的屬性大多隻提供了公開的get訪問器,所以這個類的重點在於構造方法:
```c#
public class Claim
{
// 基礎的
public Claim(string type, string value);
public Claim(string type, string value, string valueType);
public Claim(string type, string value, string valueType, string issuer);
public Claim(string type, string value, string valueType, string issuer, string originalIssuer);
//
public Claim(BinaryReader reader);
public Claim(BinaryReader reader, ClaimsIdentity subject);
}
```
暫且看一下幾個使用字元型別的建構函式引數:
1. type Claim的型別,支援自定義,但asp.net core 提供了一個基礎的型別定義:
```c#
public static class ClaimTypes
{
// 隱藏其他屬性
public const string Name = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name";
public const string Role = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role";
}
```
這個類裡定義了大多數情況下會用到的Claims的型別。
2. value 存放Claim的值,通常情況下不對這個值進行約束
3. valueType 表示 value的型別,取值範圍參考類:
```c#
public static class ClaimValueTypes
{
public const string Base64Binary = "http://www.w3.org/2001/XMLSchema#base64Binary";
public const string UpnName = "http://schemas.xmlsoap.org/claims/UPN";
public const string UpnName = "http://schemas.xmlsoap.org/claims/UPN";
public const string UInteger32 = "http://www.w3.org/2001/XMLSchema#uinteger32";
public const string Time = "http://www.w3.org/2001/XMLSchema#time";
public const string String = "http://www.w3.org/2001/XMLSchema#string";
public const string Sid = "http://www.w3.org/2001/XMLSchema#sid";
public const string RsaKeyValue = "http://www.w3.org/2000/09/xmldsig#RSAKeyValue";
public const string Rsa = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/rsa";
public const string Rfc822Name = "urn:oasis:names:tc:xacml:1.0:data-type:rfc822Name";
public const string KeyInfo = "http://www.w3.org/2000/09/xmldsig#KeyInfo";
public const string Integer64 = "http://www.w3.org/2001/XMLSchema#integer64";
public const string X500Name = "urn:oasis:names:tc:xacml:1.0:data-type:x500Name";
public const string Integer32 = "http://www.w3.org/2001/XMLSchema#integer32";
public const string HexBinary = "http://www.w3.org/2001/XMLSchema#hexBinary";
public const string Fqbn = "http://www.w3.org/2001/XMLSchema#fqbn";
public const string Email = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress";
public const string DsaKeyValue = "http://www.w3.org/2000/09/xmldsig#DSAKeyValue";
public const string Double = "http://www.w3.org/2001/XMLSchema#double";
public const string DnsName = "http://schemas.xmlsoap.org/claims/dns";
public const string DaytimeDuration = "http://www.w3.org/TR/2002/WD-xquery-operators-20020816#dayTimeDuration";
public const string DateTime = "http://www.w3.org/2001/XMLSchema#dateTime";
public const string Date = "http://www.w3.org/2001/XMLSchema#date";
public const string Boolean = "http://www.w3.org/2001/XMLSchema#boolean";
public const string Base64Octet = "http://www.w3.org/2001/XMLSchema#base64Octet";
public const string Integer = "http://www.w3.org/2001/XMLSchema#integer";
public const string YearMonthDuration = "http://www.w3.org/TR/2002/WD-xquery-operators-20020816#yearMonthDuration";
}
```
4. issuer 用來存放 Claim的釋出者,預設值是:`ClaimsIdentity.DefaultIssuer` 該值允許在建構函式是傳NULL,一旦傳NULL,則自動認為是`ClaimsIdentity.DefaultIssuer`
5. originalIssuer Claim的原發布人,如果不給值,則預設與issuer一致。
這是從建構函式以及相關文件中獲取到的。
關於ClaimTypes裡我只貼了兩個,原因是這兩個值在Claim中是兩個必不可少的值。根據屬性名就能看出來,一個是設定使用者的名稱,一個是設定使用者的角色。
那麼,繼續探索Claim裡的屬性和方法:
```c#
public class Claim
{
public string Type { get; }
public ClaimsIdentity Subject { get; }
public IDi