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

 

前言

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

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

加密扩展

IAuthenticatedEncryptor 和 IAuthenticatedEncryptorDescriptor

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

下面是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来说我也是一个循循渐进的学习过程,希望能帮助到一些人。

如果您觉得本篇文章对你有用的话,不妨点个【推荐】。


本文地址:http://www.cnblogs.com/savorboard/p/dotnetcore-data-protected-farm.html
作者博客:Savorboard
欢迎转载,请在明显位置给出出处及链接

NET Core 数据保护2的更多相关文章

  1. ASP.NET Core 数据保护(Data Protection)【中】

    前言 上篇主要是对 ASP.NET Core 的 Data Protection 做了一个简单的介绍,本篇主要是介绍一下API及使用方法. API 接口 ASP.NET Core Data Prote ...

  2. ASP.NET Core 数据保护(Data Protection)【上】

    前言 上一篇博客记录了如何在 Kestrel 中使用 HTTPS(SSL), 也是我们目前项目中实际使用到的. 数据安全往往是开发人员很容易忽略的一个部分,包括我自己.近两年业内也出现了很多因为安全问 ...

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

    前言 接[中篇],在有一些场景下,我们需要对 ASP.NET Core 的加密方法进行扩展,来适应我们的需求,这个时候就需要使用到了一些 Core 提供的高级的功能. 本文还列举了在集群场景下,有时候 ...

  4. ASP.NET Core中的数据保护

    在这篇文章中,我将介绍ASP.NET Core 数据保护系统:它是什么,为什么我们需要它,以及它如何工作. 为什么我们需要数据保护系统? 数据保护系统是ASP.NET Core使用的一组加密api.加 ...

  5. 详解Session分布式共享(.NET CORE版)

    一.前言&回顾 在上篇文章Session分布式共享 = Session + Redis + Nginx中,好多同学留言问了我好多问题,其中印象深刻的有:nginx挂了怎么办?采用Redis的S ...

  6. 集群环境下,你不得不注意的ASP.NET Core Data Protection 机制

    引言 最近线上环境遇到一个问题,就是ASP.NET Core Web应用在单个容器使用正常,扩展多个容器无法访问的问题.查看容器日志,发现以下异常: System.Security.Cryptogra ...

  7. 小白开学Asp.Net Core 《八》

    小白开学Asp.Net Core <八> — — .Net Core 数据保护组件 1.背景 我在搞(https://github.com/AjuPrince/Aju.Carefree)这 ...

  8. 在Asp.NET Core中如何优雅的管理用户机密数据

    在Asp.NET Core中如何优雅的管理用户机密数据 背景 回顾 在软件开发过程中,使用配置文件来管理某些对应用程序运行中需要使用的参数是常见的作法.在早期VB/VB.NET时代,经常使用.ini文 ...

  9. 【译】ASP.NET Core updates in .NET 5 Preview 8

    .NET 5 预览版 8 现在已经可以获取了,并且已经准备好接受评估.下面列出了本次发布的新特性: 使用 Microsoft.Identity.Web 进行 Azure Active Director ...

随机推荐

  1. poj1611 并查集

    题目链接:http://poj.org/problem?id=1611 #include <cstdio> #include <cmath> #include <algo ...

  2. oracle 零散知识汇集

    1. Select '登陆' + 2 From dual会报错: ora- 01722 无效数字,原理是oracle把'登陆'当成数字来和2进行加法运算. Select '登陆'|| 2 From d ...

  3. MySQL删除重复记录的方法

    参考网上的方法,总结了产出重复记录的方法,欢迎交流. 参考:http://www.cnblogs.com/nzbbody/p/4470638.html 方法1:创建一个新表临时储存数据 假设我们有一个 ...

  4. [javascirpt] Regex

    To Currency function toCurrency(price){ return price.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, & ...

  5. ie11只能用管理员身份打开解决办法

    解决IE11只能用管理员身份运行的问题 不知道大家有没有遇到这种情况,在毫不知情的情况下 IE11 突然打不开了,必须要用管理员身份运行才可以打开,而且重置浏览器这个方法也不奏效. 今天本人也遇到了, ...

  6. [转] Java快速教程

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Java是面向对象语言.这门语言其实相当年轻,于1995年才出现,由Sun公司出品 ...

  7. 给方法传递参数:ref参数和out参数

    /*--------------------------------------------------- 给方法传递参数:ref参数和out参数 (P106) ------------------- ...

  8. 【开源java游戏框架libgdx专题】-13-开发工具-地图的使用

    支持libGDX的地图编辑器有很多种,其中比较常用的工具为Tiled地图工具.Tiled是一款非常好用的地图编辑器.下载地址:http://www.mapeditor.org TiledMap类: 又 ...

  9. Word 查找和替换的通配符

    查找和替换的通配符 若要查找 类型 示例 任意单个字符 ? s?t 可查找“sat”和“set”. 任意字符串 * s*d 可查找“sad”和“started”. 单词的开头 < <(in ...

  10. 学习CSS一些事(上)

    p.s:这是我在学习中总结出来知识,如有不对,请多包涵.谢谢. CSS样式:行内样式,内部样式,外部样式,他们的优先级是:行内,内部,外部,遵循就近原则. 一.HTML+CSS布局分为三大类,一是流式 ...