首页 > 编程 > .NET > 正文

ASP.NET Core 数据保护(Data Protection 集群场景)下篇

2024-07-10 12:46:37
字体:
来源:转载
供稿:网友

前言 

接【中篇】 ,在有一些场景下,我们需要对 ASP.NET Core 的加密方法进行扩展,来适应我们的需求,这个时候就需要使用到了一些 Core 提供的高级的功能。 

本文还列举了在集群场景下,有时候我们需要实现自己的一些方法来对Data Protection进行分布式配置。 

加密扩展 

IAuthenticatedEncryptor 和 IAuthenticatedEncryptorDescriptor 

IAuthenticatedEncryptor是 Data Protection 在构建其密码加密系统中的一个基础的接口。
 一般情况下一个key 对应一个IAuthenticatedEncryptor,IAuthenticatedEncryptor封装了加密操作中需要使用到的秘钥材料和必要的加密算法信息等。 

下面是IAuthenticatedEncryptor接口提供的两个 api方法:
Decrypt(ArraySegment<byte> ciphertext, ArraySegment<byte> additionalAuthenticatedData) : byte[]
Encrypt(ArraySegment<byte> plaintext, ArraySegment<byte> additionalAuthenticatedData) : byte[]

其中接口中的参数additionalAuthenticatedData表示在构建加密的时候提供的一些附属信息。 

IAuthenticatedEncryptorDescriptor接口提供了一个创建包含类型信息IAuthenticatedEncryptor实例方法。

CreateEncryptorInstance() : IAuthenticatedEncryptor
ExportToXml() : XmlSerializedDescriptorInfo

密钥管理扩展 

在密钥系统管理中,提供了一个基础的接口IKey,它包含以下属性: 

Activation
creation
expiration dates
Revocation status
Key identifier (a GUID)

IKey还提供了一个创建IAuthenticatedEncryptor实例的方法CreateEncryptorInstance。 

IKeyManager接口提供了一系列用来操作Key的方法,包括存储,检索操作等。他提供的高级操作有:

 •创建一个Key 并且持久存储
 •从存储库中获取所有的 Key
 •撤销保存到存储中的一个或多个键

XmlKeyManager
通常情况下,开发人员不需要去实现IKeyManager来自定义一个 KeyManager。我们可以使用系统默认提供的XmlKeyManager类。 

XMLKeyManager是一个具体实现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);}             
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表