ASP.NET Core 數(shù)據(jù)保護(hù)(Data Protection 集群場(chǎng)景)下篇
前言
接【中篇】 ,在有一些場(chǎng)景下,我們需要對(duì) ASP.NET Core 的加密方法進(jìn)行擴(kuò)展,來(lái)適應(yīng)我們的需求,這個(gè)時(shí)候就需要使用到了一些 Core 提供的高級(jí)的功能。
本文還列舉了在集群場(chǎng)景下,有時(shí)候我們需要實(shí)現(xiàn)自己的一些方法來(lái)對(duì)Data Protection進(jìn)行分布式配置。
加密擴(kuò)展
IAuthenticatedEncryptor 和 IAuthenticatedEncryptorDescriptor
IAuthenticatedEncryptor是 Data Protection 在構(gòu)建其密碼加密系統(tǒng)中的一個(gè)基礎(chǔ)的接口。
一般情況下一個(gè)key 對(duì)應(yīng)一個(gè)IAuthenticatedEncryptor,IAuthenticatedEncryptor封裝了加密操作中需要使用到的秘鑰材料和必要的加密算法信息等。
下面是IAuthenticatedEncryptor接口提供的兩個(gè) api方法:
Decrypt(ArraySegment<byte> ciphertext, ArraySegment<byte> additionalAuthenticatedData) : byte[]
Encrypt(ArraySegment<byte> plaintext, ArraySegment<byte> additionalAuthenticatedData) : byte[]
其中接口中的參數(shù)additionalAuthenticatedData表示在構(gòu)建加密的時(shí)候提供的一些附屬信息。
IAuthenticatedEncryptorDescriptor接口提供了一個(gè)創(chuàng)建包含類型信息IAuthenticatedEncryptor實(shí)例方法。
CreateEncryptorInstance() : IAuthenticatedEncryptor
ExportToXml() : XmlSerializedDescriptorInfo
密鑰管理擴(kuò)展
在密鑰系統(tǒng)管理中,提供了一個(gè)基礎(chǔ)的接口IKey,它包含以下屬性:
Activation
creation
expiration dates
Revocation status
Key identifier (a GUID)
IKey還提供了一個(gè)創(chuàng)建IAuthenticatedEncryptor實(shí)例的方法CreateEncryptorInstance。
IKeyManager接口提供了一系列用來(lái)操作Key的方法,包括存儲(chǔ),檢索操作等。他提供的高級(jí)操作有:
•創(chuàng)建一個(gè)Key 并且持久存儲(chǔ)
•從存儲(chǔ)庫(kù)中獲取所有的 Key
•撤銷保存到存儲(chǔ)中的一個(gè)或多個(gè)鍵
XmlKeyManager
通常情況下,開發(fā)人員不需要去實(shí)現(xiàn)IKeyManager來(lái)自定義一個(gè) KeyManager。我們可以使用系統(tǒng)默認(rèn)提供的XmlKeyManager類。
XMLKeyManager是一個(gè)具體實(shí)現(xiàn)IKeyManager的類,它提供了一些非常有用的方法。
public sealed class XmlKeyManager : IKeyManager, IInternalXmlKeyManager
{
public XmlKeyManager(IXmlRepository repository, IAuthenticatedEncryptorConfiguration configuration, IServiceProvider services);
public IKey CreateNewKey(DateTimeOffset activationDate, DateTimeOffset expirationDate);
public IReadOnlyCollection<IKey> GetAllKeys();
public CancellationToken GetCacheExpirationToken();
public void RevokeAllKeys(DateTimeOffset revocationDate, string reason = null);
public void RevokeKey(Guid keyId, string reason = null);
}
•IAuthenticatedEncryptorConfiguration 主要是規(guī)定新 Key 使用的算法。
•IXmlRepository 主要控制 Key 在哪里持久化存儲(chǔ)。
IXmlRepository
IXmlRepository接口主要提供了持久化以及檢索XML的方法,它只要提供了兩個(gè)API:
•GetAllElements() : IReadOnlyCollection
•StoreElement(XElement element, string friendlyName)
我們可以通過(guò)實(shí)現(xiàn)IXmlRepository接口的StoreElement方法來(lái)定義data protection xml的存儲(chǔ)位置。
GetAllElements來(lái)檢索所有存在的加密的xml文件。
接口部分寫到這里吧,因?yàn)檫@一篇我想把重點(diǎn)放到下面,更多接口的介紹大家還是去官方文檔看吧~
集群場(chǎng)景
上面的API估計(jì)看著有點(diǎn)枯燥,那我們就來(lái)看看我們需要在集群場(chǎng)景下借助于Data Protection來(lái)做點(diǎn)什么吧。
就像我在【上篇】總結(jié)中末尾提到的,在做分布式集群的時(shí)候,Data Protection的一些機(jī)制我們需要知道,因?yàn)槿绻涣私膺@些可能會(huì)給你的部署帶來(lái)一些麻煩,下面我們就來(lái)看看吧。
在做集群的時(shí),我們必須知道并且明白關(guān)于 ASP.NET Core Data Protection 的三個(gè)東西:
1、程序識(shí)別者
“Application discriminator”,它是用來(lái)標(biāo)識(shí)應(yīng)用程序的唯一性。
為什么需要這個(gè)東西呢?因?yàn)樵诩涵h(huán)境中,如果不被具體的硬件機(jī)器環(huán)境所限制,就要排除運(yùn)行機(jī)器的一些差異,就需要抽象出來(lái)一些特定的標(biāo)識(shí),來(lái)標(biāo)識(shí)應(yīng)用程序本身并且使用該標(biāo)識(shí)來(lái)區(qū)分不同的應(yīng)用程序。這個(gè)時(shí)候,我們可以指定ApplicationDiscriminator。
在services.AddDataProtection(DataProtectionOptions option)的時(shí)候,ApplicationDiscriminator可以作為參數(shù)傳遞,來(lái)看一下代碼:
public void ConfigureServices(IServiceCollection services)
{
services.AddDataProtection();
services.AddDataProtection(DataProtectionOptions option);
}
//===========擴(kuò)展方法如下:
public static class DataProtectionServiceCollectionExtensions
{
public static IDataProtectionBuilder AddDataProtection(this IServiceCollection services);
//具有可傳遞參數(shù)的重載,在集群環(huán)境中需要使用此項(xiàng)配置
public static IDataProtectionBuilder AddDataProtection(this IServiceCollection services, Action<DataProtectionOptions> setupAction);
}
// DataProtectionOptions 屬性:
public class DataProtectionOptions
{
public string ApplicationDiscriminator { get; set; }
}
可以看到這個(gè)擴(kuò)展返回的是一個(gè)IDataProtectionBuilder,在IDataProtectionBuilder還有一個(gè)擴(kuò)展方法叫 SetApplicationName ,這個(gè)擴(kuò)展方法在內(nèi)部還是修改的ApplicationDiscriminator的值。也就說(shuō)以下寫法是等價(jià)的:
services.AddDataProtection(x => x.ApplicationDiscriminator = "my_app_sample_identity");
services.AddDataProtection().SetApplicationName("my_app_sample_identity");
也就是說(shuō)集群環(huán)境下同一應(yīng)用程序他們需要設(shè)定為相同的值(ApplicationName or ApplicationDiscriminator)。
2、主加密鍵
“Master encryption key”,主要是用來(lái)加密解密的,包括一客戶端服務(wù)器在請(qǐng)求的過(guò)程中的一些會(huì)話數(shù)據(jù),狀態(tài)等。有幾個(gè)可選項(xiàng)可以配置,比如使用證書或者是windows DPAPI或者注冊(cè)表等。如果是非windows平臺(tái),注冊(cè)表和Windows DPAPI就不能用了。
public void ConfigureServices(IServiceCollection services)
{
services.AddDataProtection()
//windows dpaip 作為主加密鍵
.ProtectKeysWithDpapi()
//如果是 windows 8+ 或者windows server2012+ 可以使用此選項(xiàng)(基于Windows DPAPI-NG)
.ProtectKeysWithDpapiNG("SID={current account SID}", DpapiNGProtectionDescriptorFlags.None)
//如果是 windows 8+ 或者windows server2012+ 可以使用此選項(xiàng)(基于證書)
.ProtectKeysWithDpapiNG("CERTIFICATE=HashId:3BCE558E2AD3E0E34A7743EAB5AEA2A9BD2575A0", DpapiNGProtectionDescriptorFlags.None)
//使用證書作為主加密鍵,目前只有widnows支持,linux還不支持。
.ProtectKeysWithCertificate();
}
如果在集群環(huán)境中,他們需要具有配置相同的主加密鍵。
3、加密后存儲(chǔ)位置
在【上篇】的時(shí)候說(shuō)過(guò),默認(rèn)情況下Data Protection會(huì)生成 xml 文件用來(lái)存儲(chǔ)session或者是狀態(tài)的密鑰文件。這些文件用來(lái)加密或者解密session等狀態(tài)數(shù)據(jù)。
就是上篇中說(shuō)的那個(gè)私鑰存儲(chǔ)位置:
1、如果程序寄宿在 Microsoft Azure下,存儲(chǔ)在“%HOME%\ASP.NET\DataProtection-Keys” 文件夾。
2、如果程序寄宿在IIS下,它被保存在HKLM注冊(cè)表的ACLed特殊注冊(cè)表鍵,并且只有工作進(jìn)程可以訪問(wèn),它使用windows的DPAPI加密。
3、如果當(dāng)前用戶可用,即win10或者win7中,它存儲(chǔ)在“%LOCALAPPDATA%\ASP.NET\DataProtection-Keys”文件夾,同樣使用的windows的DPAPI加密。
4、如果這些都不符合,那么也就是私鑰是沒(méi)有被持久化的,也就是說(shuō)當(dāng)進(jìn)程關(guān)閉的時(shí)候,生成的私鑰就丟失了。
集群環(huán)境下:
最簡(jiǎn)單的方式是通過(guò)文件共享、DPAPI或者注冊(cè)表,也就是說(shuō)把加密過(guò)后的xml文件都存儲(chǔ)在相同的地方。為什么說(shuō)最簡(jiǎn)單,因?yàn)橄到y(tǒng)已經(jīng)給封裝好了,不需要寫多余的代碼了,但是要保證文件共享相關(guān)的端口是開放的。如下:
public void ConfigureServices(IServiceCollection services)
{
services.AddDataProtection()
//windows、Linux、macOS 下可以使用此種方式 保存到文件系統(tǒng)
.PersistKeysToFileSystem(new System.IO.DirectoryInfo("C:\\share_keys\\"))
//windows 下可以使用此種方式 保存到注冊(cè)表
.PersistKeysToRegistry(Microsoft.Win32.RegistryKey.FromHandle(null))
}
你也可以自己擴(kuò)展方法來(lái)自己定義一些存儲(chǔ),比如使用數(shù)據(jù)庫(kù)或者Redis等。
不過(guò)通常情況下,如果在linux上部署的話,都是需要擴(kuò)展的。下面來(lái)看一下我們想要用redis存儲(chǔ),該怎么做呢?
如何擴(kuò)展加密鍵集合的存儲(chǔ)位置?
首先,定義個(gè)針對(duì)IXmlRepository接口的 redis 實(shí)現(xiàn)類RedisXmlRepository.cs:
public class RedisXmlRepository : IXmlRepository, IDisposable
{
public static readonly string RedisHashKey = "DataProtectionXmlRepository";
private IConnectionMultiplexer _connection;
private bool _disposed = false;
public RedisXmlRepository(string connectionString, ILogger<RedisXmlRepository> logger)
: this(ConnectionMultiplexer.Connect(connectionString), logger)
{
}
public RedisXmlRepository(IConnectionMultiplexer connection, ILogger<RedisXmlRepository> logger)
{
if (connection == null)
{
throw new ArgumentNullException(nameof(connection));
}
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}
this._connection = connection;
this.Logger = logger;
var configuration = Regex.Replace(this._connection.Configuration, @"password\s*=\s*[^,]*", "password=****", RegexOptions.IgnoreCase);
this.Logger.LogDebug("Storing data protection keys in Redis: {RedisConfiguration}", configuration);
}
public ILogger<RedisXmlRepository> Logger { get; private set; }
public void Dispose()
{
this.Dispose(true);
}
public IReadOnlyCollection<XElement> GetAllElements()
{
var database = this._connection.GetDatabase();
var hash = database.HashGetAll(RedisHashKey);
var elements = new List<XElement>();
if (hash == null || hash.Length == 0)
{
return elements.AsReadOnly();
}
foreach (var item in hash.ToStringDictionary())
{
elements.Add(XElement.Parse(item.Value));
}
this.Logger.LogDebug("Read {XmlElementCount} XML elements from Redis.", elements.Count);
return elements.AsReadOnly();
}
public void StoreElement(XElement element, string friendlyName)
{
if (element == null)
{
throw new ArgumentNullException(nameof(element));
}
if (string.IsNullOrEmpty(friendlyName))
{
friendlyName = Guid.NewGuid().ToString();
}
this.Logger.LogDebug("Storing XML element with friendly name {XmlElementFriendlyName}.", friendlyName);
this._connection.GetDatabase().HashSet(RedisHashKey, friendlyName, element.ToString());
}
protected virtual void Dispose(bool disposing)
{
if (!this._disposed)
{
if (disposing)
{
if (this._connection != null)
{
this._connection.Close();
this._connection.Dispose();
}
}
this._connection = null;
this._disposed = true;
}
}
}
然后任意一個(gè)擴(kuò)展類中先定義一個(gè)擴(kuò)展方法:
public static IDataProtectionBuilder PersistKeysToRedis(this IDataProtectionBuilder builder, string redisConnectionString)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
if (redisConnectionString == null)
{
throw new ArgumentNullException(nameof(redisConnectionString));
}
if (redisConnectionString.Length == 0)
{
throw new ArgumentException("Redis connection string may not be empty.", nameof(redisConnectionString));
}
//因?yàn)樵趕ervices.AddDataProtection()的時(shí)候,已經(jīng)注入了IXmlRepository,所以應(yīng)該先移除掉
//此處應(yīng)該封裝成為一個(gè)方法來(lái)調(diào)用,為了讀者好理解,我就直接寫了
for (int i = builder.Services.Count - 1; i >= 0; i--)
{
if (builder.Services[i]?.ServiceType == descriptor.ServiceType)
{
builder.Services.RemoveAt(i);
}
}
var descriptor = ServiceDescriptor.Singleton<IXmlRepository>(services => new RedisXmlRepository(redisConnectionString, services.GetRequiredService<ILogger<RedisXmlRepository>>()))
builder.Services.Add(descriptor);
return builder.Use();
}
最終Services中關(guān)于DataProtection是這樣的:
public void ConfigureServices(IServiceCollection services)
{
services.AddDataProtection()
// ================以下是唯一標(biāo)識(shí)==============
//設(shè)置應(yīng)用程序唯一標(biāo)識(shí)
.SetApplicationName("my_app_sample_identity");
// =============以下是主加密鍵===============
//windows dpaip 作為主加密鍵
.ProtectKeysWithDpapi()
//如果是 windows 8+ 或者windows server2012+ 可以使用此選項(xiàng)(基于Windows DPAPI-NG)
.ProtectKeysWithDpapiNG("SID={current account SID}", DpapiNGProtectionDescriptorFlags.None)
//如果是 windows 8+ 或者windows server2012+ 可以使用此選項(xiàng)(基于證書)
.ProtectKeysWithDpapiNG("CERTIFICATE=HashId:3BCE558E2AD3E0E34A7743EAB5AEA2A9BD2575A0", DpapiNGProtectionDescriptorFlags.None)
//使用證書作為主加密鍵,目前只有widnows支持,linux還不支持。
.ProtectKeysWithCertificate();
// ==============以下是存儲(chǔ)位置=================
//windows、Linux、macOS 下可以使用此種方式 保存到文件系統(tǒng)
.PersistKeysToFileSystem(new System.IO.DirectoryInfo("C:\\share_keys\\"))
//windows 下可以使用此種方式 保存到注冊(cè)表
.PersistKeysToRegistry(Microsoft.Win32.RegistryKey.FromHandle(null))
// 存儲(chǔ)到redis
.PersistKeysToRedis(Configuration.Section["RedisConnection"])
}
在上面的配置中,我把所有可以使用的配置都列出來(lái)了哦,實(shí)際項(xiàng)目中應(yīng)該視實(shí)際情況選擇。
總結(jié)
關(guān)于ASP.NET Core Data Protection 系列終于寫完了,其實(shí)這這部分花了蠻多時(shí)間的,對(duì)于Data Protection來(lái)說(shuō)我也是一個(gè)循循漸進(jìn)的學(xué)習(xí)過(guò)程,希望能幫助到一些人。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ASP.NET Core 數(shù)據(jù)保護(hù)(Data Protection)中篇
- ASP.NET Core 數(shù)據(jù)保護(hù)(Data Protection)上篇
- ASP.NET Core Kestrel 中使用 HTTPS (SSL)
- ASP.NET Core集成微信登錄
- 微信搶紅包ASP.NET代碼輕松實(shí)現(xiàn)
- 基于ASP.NET實(shí)現(xiàn)日期轉(zhuǎn)為大寫的漢字
- ASP.NET?MVC5網(wǎng)站開發(fā)之用戶資料的修改和刪除3(七)
- ASP.NET?MVC5網(wǎng)站開發(fā)之用戶添加和瀏覽2(七)
- ASP.NET?MVC5網(wǎng)站開發(fā)之用戶角色的后臺(tái)管理1(七)
- ASP.NET 程序員都非常有用的85個(gè)工具
相關(guān)文章
Asp.Net中的Action和Func委托實(shí)現(xiàn)
這篇文章主要介紹了Asp.Net中的Action和Func委托的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
ASP.NET?Core構(gòu)建OData查詢Restful?API
這篇文章主要為大家介紹了ASP.NET?Core建構(gòu)OData實(shí)體模型實(shí)現(xiàn)Restful?API查詢,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
ASP.NET MVC中Controller控制器向View視圖傳值的幾種方式
這篇文章介紹了ASP.NET MVC中Controller控制器向View視圖傳值的幾種方式,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
jQuery 插件autocomplete自動(dòng)完成應(yīng)用(自動(dòng)補(bǔ)全)(asp.net后臺(tái))
項(xiàng)目中有時(shí)會(huì)用到自動(dòng)補(bǔ)全查詢,就像Google搜索框、淘寶商品搜索功能,輸入漢字或字母,則以該漢字或字母開頭的相關(guān)條目會(huì)顯示出來(lái)供用戶選擇, autocomplete插件就是完成這樣的功能2011-10-10
asp.net編程實(shí)現(xiàn)刪除文件夾及文件夾下文件的方法
這篇文章主要介紹了asp.net編程實(shí)現(xiàn)刪除文件夾及文件夾下文件的方法,涉及asp.net針對(duì)文件與目錄的遍歷及刪除操作實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11

