ASP.NET Core 数据保护(Data Protection 集群场景)下篇
前言
接【中篇】 ,在有一些场景下,我们需要对 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); }
•IAuthenticatedEncryptorConfiguration 主要是规定新 Key 使用的算法。
•IXmlRepository 主要控制 Key 在哪里持久化存储。
IXmlRepository
IXmlRepository接口主要提供了持久化以及检索XML的方法,它只要提供了两个API:
•GetAllElements() : IReadOnlyCollection
•StoreElement(XElement element, string friendlyName)
我们可以通过实现IXmlRepository接口的StoreElement方法来定义data protection xml的存储位置。
GetAllElements来检索所有存在的加密的xml文件。
接口部分写到这里吧,因为这一篇我想把重点放到下面,更多接口的介绍大家还是去官方文档看吧~
集群场景
上面的API估计看着有点枯燥,那我们就来看看我们需要在集群场景下借助于Data Protection来做点什么吧。
就像我在【上篇】总结中末尾提到的,在做分布式集群的时候,Data Protection的一些机制我们需要知道,因为如果不了解这些可能会给你的部署带来一些麻烦,下面我们就来看看吧。
在做集群的时,我们必须知道并且明白关于 ASP.NET Core Data Protection 的三个东西:
1、程序识别者
“Application discriminator”,它是用来标识应用程序的唯一性。
为什么需要这个东西呢?因为在集群环境中,如果不被具体的硬件机器环境所限制,就要排除运行机器的一些差异,就需要抽象出来一些特定的标识,来标识应用程序本身并且使用该标识来区分不同的应用程序。这个时候,我们可以指定ApplicationDiscriminator。
在services.AddDataProtection(DataProtectionOptions option)的时候,ApplicationDiscriminator可以作为参数传递,来看一下代码:
public void ConfigureServices(IServiceCollection services) { services.AddDataProtection(); services.AddDataProtection(DataProtectionOptions option); } //===========扩展方法如下: public static class DataProtectionServiceCollectionExtensions { public static IDataProtectionBuilder AddDataProtection(this IServiceCollection services); //具有可传递参数的重载,在集群环境中需要使用此项配置 public static IDataProtectionBuilder AddDataProtection(this IServiceCollection services, Action<DataProtectionOptions> setupAction); } // DataProtectionOptions 属性: public class DataProtectionOptions { public string ApplicationDiscriminator { get; set; } }
可以看到这个扩展返回的是一个IDataProtectionBuilder,在IDataProtectionBuilder还有一个扩展方法叫 SetApplicationName ,这个扩展方法在内部还是修改的ApplicationDiscriminator的值。也就说以下写法是等价的:
services.AddDataProtection(x => x.ApplicationDiscriminator = "my_app_sample_identity");
services.AddDataProtection().SetApplicationName("my_app_sample_identity");
也就是说集群环境下同一应用程序他们需要设定为相同的值(ApplicationName or ApplicationDiscriminator)。
2、主加密键
“Master encryption key”,主要是用来加密解密的,包括一客户端服务器在请求的过程中的一些会话数据,状态等。有几个可选项可以配置,比如使用证书或者是windows DPAPI或者注册表等。如果是非windows平台,注册表和Windows DPAPI就不能用了。
public void ConfigureServices(IServiceCollection services) { services.AddDataProtection() //windows dpaip 作为主加密键 .ProtectKeysWithDpapi() //如果是 windows 8+ 或者windows server2012+ 可以使用此选项(基于Windows DPAPI-NG) .ProtectKeysWithDpapiNG("SID={current account SID}", DpapiNGProtectionDescriptorFlags.None) //如果是 windows 8+ 或者windows server2012+ 可以使用此选项(基于证书) .ProtectKeysWithDpapiNG("CERTIFICATE=HashId:3BCE558E2AD3E0E34A7743EAB5AEA2A9BD2575A0", DpapiNGProtectionDescriptorFlags.None) //使用证书作为主加密键,目前只有widnows支持,linux还不支持。 .ProtectKeysWithCertificate(); }
如果在集群环境中,他们需要具有配置相同的主加密键。
3、加密后存储位置
在【上篇】的时候说过,默认情况下Data Protection会生成 xml 文件用来存储session或者是状态的密钥文件。这些文件用来加密或者解密session等状态数据。
就是上篇中说的那个私钥存储位置:
1、如果程序寄宿在 Microsoft Azure下,存储在“%HOME%\ASP.NET\DataProtection-Keys” 文件夹。
2、如果程序寄宿在IIS下,它被保存在HKLM注册表的ACLed特殊注册表键,并且只有工作进程可以访问,它使用windows的DPAPI加密。
3、如果当前用户可用,即win10或者win7中,它存储在“%LOCALAPPDATA%\ASP.NET\DataProtection-Keys”文件夹,同样使用的windows的DPAPI加密。
4、如果这些都不符合,那么也就是私钥是没有被持久化的,也就是说当进程关闭的时候,生成的私钥就丢失了。
集群环境下:
最简单的方式是通过文件共享、DPAPI或者注册表,也就是说把加密过后的xml文件都存储在相同的地方。为什么说最简单,因为系统已经给封装好了,不需要写多余的代码了,但是要保证文件共享相关的端口是开放的。如下:
public void ConfigureServices(IServiceCollection services) { services.AddDataProtection() //windows、Linux、macOS 下可以使用此种方式 保存到文件系统 .PersistKeysToFileSystem(new System.IO.DirectoryInfo("C:\\share_keys\\")) //windows 下可以使用此种方式 保存到注册表 .PersistKeysToRegistry(Microsoft.Win32.RegistryKey.FromHandle(null)) }
你也可以自己扩展方法来自己定义一些存储,比如使用数据库或者Redis等。
不过通常情况下,如果在linux上部署的话,都是需要扩展的。下面来看一下我们想要用redis存储,该怎么做呢?
如何扩展加密键集合的存储位置?
首先,定义个针对IXmlRepository接口的 redis 实现类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; } } }
然后任意一个扩展类中先定义一个扩展方法:
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)); } //因为在services.AddDataProtection()的时候,已经注入了IXmlRepository,所以应该先移除掉 //此处应该封装成为一个方法来调用,为了读者好理解,我就直接写了 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中关于DataProtection是这样的:
public void ConfigureServices(IServiceCollection services) { services.AddDataProtection() // ================以下是唯一标识============== //设置应用程序唯一标识 .SetApplicationName("my_app_sample_identity"); // =============以下是主加密键=============== //windows dpaip 作为主加密键 .ProtectKeysWithDpapi() //如果是 windows 8+ 或者windows server2012+ 可以使用此选项(基于Windows DPAPI-NG) .ProtectKeysWithDpapiNG("SID={current account SID}", DpapiNGProtectionDescriptorFlags.None) //如果是 windows 8+ 或者windows server2012+ 可以使用此选项(基于证书) .ProtectKeysWithDpapiNG("CERTIFICATE=HashId:3BCE558E2AD3E0E34A7743EAB5AEA2A9BD2575A0", DpapiNGProtectionDescriptorFlags.None) //使用证书作为主加密键,目前只有widnows支持,linux还不支持。 .ProtectKeysWithCertificate(); // ==============以下是存储位置================= //windows、Linux、macOS 下可以使用此种方式 保存到文件系统 .PersistKeysToFileSystem(new System.IO.DirectoryInfo("C:\\share_keys\\")) //windows 下可以使用此种方式 保存到注册表 .PersistKeysToRegistry(Microsoft.Win32.RegistryKey.FromHandle(null)) // 存储到redis .PersistKeysToRedis(Configuration.Section["RedisConnection"]) }
在上面的配置中,我把所有可以使用的配置都列出来了哦,实际项目中应该视实际情况选择。
总结
关于ASP.NET Core Data Protection 系列终于写完了,其实这这部分花了蛮多时间的,对于Data Protection来说我也是一个循循渐进的学习过程,希望能帮助到一些人。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。
相关文章
- 这篇文章主要为大家详细介绍了ASP.NET购物车的实现过程,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-22
- 这篇文章主要介绍了.NET Core下使用Kafka的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22
- 在开发过程中,使用Visual Studio的断点调试功能可以很方便帮我们调试发现程序存在的错误,同样Visual Studio也支持对SQL Server里面的存储过程进行调试,下面就让我们看看具体的调试方法。...2021-09-22
- 这篇文章主要介绍了详解.NET Core 3.0 里新的JSON API,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22
ASP.NET Core根据环境变量支持多个 appsettings.json配置文件
这篇文章主要介绍了ASP.NET Core根据环境变量支持多个 appsettings.json配置文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22- 这篇文章主要介绍了记一次EFCore类型转换错误及解决方案,帮助大家更好的理解和学习使用asp.net core,感兴趣的朋友可以了解下...2021-09-22
详解ASP.NET Core 中基于工厂的中间件激活的实现方法
这篇文章主要介绍了ASP.NET Core 中基于工厂的中间件激活的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-09-22asp.net通过消息队列处理高并发请求(以抢小米手机为例)
这篇文章主要介绍了asp.net通过消息队列处理高并发请求(以抢小米手机为例),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22- Underscore 是一个 JavaScript 工具库,它提供了一整套函数式编程的实用功能,但是没有扩展任何 JavaScript 内置对象。这篇文章主要介绍了underscore源码分析相关知识,感兴趣的朋友一起学习吧...2016-01-02
ASP.NET单选按钮控件RadioButton常用属性和方法介绍
RadioButton又称单选按钮,其在工具箱中的图标为 ,单选按钮通常成组出现,用于提供两个或多个互斥选项,即在一组单选钮中只能选择一个...2021-09-22ASP.NET 2.0中的数据操作:使用两个DropDownList过滤的主/从报表
在前面的指南中我们研究了如何显示一个简单的主/从报表, 该报表使用DropDownList和GridView控件, DropDownList填充类别,GridView显示选定类别的产品. 这类报表用于显示具有...2016-05-19详解.NET Core 使用HttpClient SSL请求出错的解决办法
这篇文章主要介绍了.NET Core 使用HttpClient SSL请求出错的解决办法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2021-09-22ASP.NET中iframe框架点击左边页面链接 右边显示链接页面内容
这篇文章主要介绍了ASP.NET中iframe框架点击左边页面链接,右边显示链接页面内容的实现代码,感兴趣的小伙伴们可以参考一下...2021-09-22- ASP.NET Web API具有与ASP.NET MVC类似的编程方式,ASP.NET Web API不仅仅具有一个完全独立的消息处理管道,而且这个管道比为ASP.NET MVC设计的管道更为复杂,功能也更为强大。下面创建一个简单的Web API项目,需要的朋友可以参考下...2021-09-22
- 这篇文章主要介绍了ASP.NET连接MySql数据库的2个方法及示例,使用的是MySQL官方组件和ODBC.NET,需要的朋友可以参考下...2021-09-22
- 这篇文章主要介绍了Asp.Net使用Bulk实现批量插入数据的方法,对于进行asp.net数据库程序设计非常有借鉴价值,需要的朋友可以参考下...2021-09-22
在ASP.NET 2.0中操作数据之二十九:用DataList和Repeater来显示数据
本文主要讲解ASP.NET 2.0中如何使用DataList 和 Repeater 来呈现数据,DataList包含一个table标记,而Repeater不会添加任何额外的代码,个人在实际开发中更推荐使用Repeater。...2021-09-22- 这篇文章主要介绍了获取DataTable选择第一行某一列值,需要的朋友可以参考下...2021-09-22
- 这篇文章主要介绍了ASP.Net中的async+await异步编程的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22
- 这篇文章介绍了Asp.net动态生成html页面的方法,有需要的朋友可以参考一下...2021-09-22