引言
最近線上環境遇到一個問題,就是ASP.NET Core Web應用在單個容器使用正常,擴展多個容器無法訪問的問題。查看容器日志,發現以下異常:
System.Security.Cryptography.CryptographicException: The key {efbb9f35-3a49-4f7f-af19-0f888fb3e04b} was not found in the key ring.
2019-09-30T18:34:55.473037193+08:00 at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.UnprotectCore(Byte[] protectedData, Boolean allowOperationsOnRevokedKeys, UnprotectStatus& status)
2019-09-30T18:34:55.473046762+08:00 at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.DangerousUnprotect(Byte[] protectedData, Boolean ignoreRevocationErrors, Boolean& requiresMigration, Boolean& wasRevoked)
2019-09-30T18:34:55.473055477+08:00 at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.Unprotect(Byte[] protectedData)
2019-09-30T18:34:55.473064427+08:00 at Microsoft.AspNetCore.Session.CookieProtection.Unprotect(IDataProtector protector, String protectedText, ILogger logger)
通過排查,發現了是由於 ASP.NET Core Data Protection 機制引起的。
Data Protection 機制
對於Data Protection機制,曉東大大已經有系列文章詳述了,我這里就不再過多贅述,只簡單總結一下。需要了解詳細的機制,建議閱讀以下系列文章:
ASP.NET Core 數據保護(Data Protection)【上】
ASP.NET Core 數據保護(Data Protection)【中】
ASP.NET Core 數據保護(Data Protection 集群場景)【下】
Data Protection(數據安全)機制:為了確保Web應用敏感數據的安全存儲,該機制提供了一個簡單、基於非對稱加密改進的、性能良好的、開箱即用的加密API用於數據保護。
它不需要開發人員自行生成密鑰,它會根據當前應用的運行環境,生成該應用獨有的一個私鑰。這在單一部署的情況下沒有問題。
一旦在集群環境下進行水平擴展,那么每個獨立的應用都有一個獨立的私鑰。這樣在負載均衡時,一個請求先在A容器建立的Session會話,該機制會通過當前容器的密鑰加密Cookie寫入到客戶端,下個請求路由到B容器,攜帶的Cookie在B容器是無法通過B容器的密鑰進行解密。
進而會導致會話信息丟失的問題。所以在集群情況下,為了確保加密數據的互通,應用必須共享私鑰。
私鑰共享
這里以使用Redis來共享私鑰舉例,添加Microsoft.AspNetCore.DataProtection.StackExchangeRedis Nuget包用於存儲密鑰。
添加Microsoft.Extensions.Caching.StackExchangeRedisNuget包用於配置分布式Session。
public IServiceProvider ConfigureServices(IServiceCollection services)
{
//獲取Redis 連接字符串
var redisConnStr = this.Configuration.GetValue<string>(SigeAppSettings.Redis_Endpoint);
var redis = ConnectionMultiplexer.Connect(redisConnStr);//建立Redis 連接
//添加數據保護服務,設置統一應用程序名稱,並指定使用Reids存儲私鑰
services.AddDataProtection()
.SetApplicationName(Assembly.GetExecutingAssembly().FullName)
.PersistKeysToStackExchangeRedis(redis, "DataProtection-Keys");
//添加Redis緩存用於分布式Session
services.AddStackExchangeRedisCache(options =>
{
options.Configuration = redisConnStr;
options.InstanceName =Assembly.GetExecutingAssembly().FullName;
});
//添加Session
services.AddSession(options =>
{
options.Cookie.Name = Assembly.GetExecutingAssembly().FullName;
options.IdleTimeout = TimeSpan.FromMinutes(20);//設置session的過期時間
options.Cookie.HttpOnly = true;//設置在瀏覽器不能通過js獲得該cookie的值
options.Cookie.IsEssential = true;
}
);
}
