數據分類可協助您根據其敏感度和保護層級來分類(或分類)數據。 DataClassification 結構可讓您為敏感資訊加上標籤,並根據這些標籤強制執行政策。
- DataClassification.TaxonomyName:識別分類系統。
- DataClassification.Value:代表分類法內的特定標籤。
在某些情況下,您可能需要明確表示數據沒有分類,這可透過 DataClassification.None來完成。 同樣地,您可能需要指定資料分類未知—在這些情況下使用 DataClassification.Unknown。
安裝套件
若要開始使用,請安裝 📦 Microsoft.Extensions.Compliance.Abstractions NuGet 套件:
dotnet add package Microsoft.Extensions.Compliance.Abstractions
或者,如果您使用 .NET 10+ SDK:
dotnet package add Microsoft.Extensions.Compliance.Abstractions
建立自訂分類
為不同類型的敏感數據建立 static 成員,以定義自定義分類。 這可讓您以一致的方式標記和處理整個應用程式的數據。 請考慮下列範例類別:
using Microsoft.Extensions.Compliance.Classification;
internal static class MyTaxonomyClassifications
{
internal static string Name => "MyTaxonomy";
internal static DataClassification PrivateInformation => new(Name, nameof(PrivateInformation));
internal static DataClassification CreditCardNumber => new(Name, nameof(CreditCardNumber));
internal static DataClassification SocialSecurityNumber => new(Name, nameof(SocialSecurityNumber));
internal static DataClassificationSet PrivateAndSocialSet => new(PrivateInformation, SocialSecurityNumber);
}
如果您要與其他應用程式共用自訂分類法,這個類別及其成員應該 public,而不是 internal。 例如,您可以有一個包含自定義分類的共享連結庫,您可以在多個應用程式中使用。
DataClassificationSet 可讓您將多個數據分類組成單一集合。 這可讓您使用多個數據分類來分類數據。 此外,.NET 編輯 API 會使用 DataClassificationSet。
備註
多個數據分類會一起 DataClassificationSet 視為單一分類。 您可以將它視為邏輯 AND 作業。 例如,如果您針對分類為 DataClassificationSet 和 PrivateInformationSocialSecurityNumber的數據設定修訂,則不會套用至只分類為 PrivateInformation 或僅限 SocialSecurityNumber的數據。
建立自訂分類屬性
根據您的自定義分類建立自定義屬性。 使用這些屬性以正確的分類標記您的數據。 請考慮下列自訂屬性類別定義:
public sealed class PrivateInformationAttribute : DataClassificationAttribute
{
public PrivateInformationAttribute()
: base(MyTaxonomyClassifications.PrivateInformation)
{
}
}
上述程式代碼會宣告私用資訊屬性,這是 DataClassificationAttribute 類型的子類別。 它會定義無參數建構函式,並將自訂 DataClassification 傳遞至其 base。
綁定數據分類設定
若要系結您的數據分類設定,請使用 .NET 組態系統。 例如,假設您使用 JSON 組態提供者,您的 appsettings.json 可以定義如下:
{
"Key": {
"PhoneNumber": "MyTaxonomy:PrivateInformation",
"ExampleDictionary": {
"CreditCard": "MyTaxonomy:CreditCardNumber",
"SSN": "MyTaxonomy:SocialSecurityNumber"
}
}
}
現在,請考慮下列選項模式方法,將這些組態設定系結至 TestOptions 物件:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Compliance.Classification;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
public class TestOptions
{
public DataClassification? PhoneNumber { get; set; }
public IDictionary<string, DataClassification> ExampleDictionary { get; set; } = new Dictionary<string, DataClassification>();
}
class Program
{
static void Main(string[] args)
{
// Build configuration from an external json file.
IConfiguration configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
// Setup DI container and bind the configuration section "Key" to TestOptions.
IServiceCollection services = new ServiceCollection();
services.Configure<TestOptions>(configuration.GetSection("Key"));
// Build the service provider.
IServiceProvider serviceProvider = services.BuildServiceProvider();
// Get the bound options.
TestOptions options = serviceProvider.GetRequiredService<IOptions<TestOptions>>().Value;
// Simple output demonstrating binding results.
Console.WriteLine("Configuration bound to TestOptions:");
Console.WriteLine($"PhoneNumber: {options.PhoneNumber}");
foreach (var item in options.ExampleDictionary)
{
Console.WriteLine($"{item.Key}: {item.Value}");
}
}
}