c# - S3 的数据保护密钥环在点网核心中无法正常工作
问题描述
我们正在使用带有 dotnet core 2.2.0 版本的 MVC。对于数据保护,我们使用 AWS S3 作为持久存储。Web 应用程序作为 Windows 服务托管在负载均衡器后面的两台机器上。身份验证基于 cookie。在两台机器上,我们对 S3 上的存储密钥环进行了以下配置。
"AwsProfile": true,
"Location": "s3-bucket-name",
"UseS3Protection": true,
"KeyPrefix": "keys/",
"CanMachineGenerateKeyRing" : true
CanMachineGenerateKeyRing 在两台机器上都是 true。添加数据保护的代码示例如下:
services.AddDataProtection().SetApplicationName("app")
.SetDefaultKeyLifetime(TimeSpan.FromDays(10))
.PersistKeysToAwsS3(CreateAmazonS3Client(configuration),
.new S3XmlRepositoryConfig(configuration["Location"])
{
KeyPrefix = configuration["KeyPrefix"]
})
.ProtectKeysWithCertificate(certificate1)
.UnprotectKeysWithAnyCertificate(certificate1);
添加数据保护层后,我们添加了 cookie 身份验证层,如下所示:
services.AddAuthentication(options =>
{
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
options.Cookie.Name = "cookie1";
options.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.None;
options.DataProtectionProvider = DataProtectionProvider.Create("app");
options.DataProtectionProvider.CreateProtector("app");
options.Cookie.Path = "/";
options.Cookie.IsEssential = true;
options.Cookie.Expiration = TimeSpan.FromMinutes(60);
options.ExpireTimeSpan =
TimeSpan.FromMinutes(60);
options.Cookie.SecurePolicy = Microsoft.AspNetCore.Http.CookieSecurePolicy.Always;
options.LoginPath = "/login";
options.Validate();
});
现在,问题是:用户身份验证成功后,身份验证cookie登陆浏览器。每当我向服务器请求受保护资源时,一些请求处理成功,其中一些请求失败,状态为 403。当我看到日志时,它说
Level":"Information","MessageTemplate":"{AuthenticationScheme} 未通过身份验证。失败消息:{FailureMessage}","Properties":{"AuthenticationScheme":"Cookies","FailureMessage":"Unprotect ticket failed","EventId":{"Id":7},"SourceContext":"Microsoft. AspNetCore.Authentication.Cookies.CookieAuthenticationHandler
{"Timestamp":"2020-01-24T20:14:10.6128310+00:00","Level":"Warning","MessageTemplate":"解除会话 cookie 保护时出错。","Exception":"System.Security .Cryptography.CryptographicException:在密钥环中找不到密钥 {d21e65c4-b7f0-41dd-903b-ab11f4a788d0}。\r\n 在 Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.UnprotectCore(Byte[] protectedData, Boolean allowOperationsOnRevokedKeys , UnprotectStatus& 状态)\r\n 在 Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.DangerousUnprotect(Byte[] protectedData, Boolean ignoreRevocationErrors, Boolean& requiresMigration, Boolean& wasRevoked)\r\n 在 Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector。取消保护(字节 [] 受保护数据)
一种奇怪的行为是 cookie 不受只有一台创建它的机器的保护。请帮忙,我不知道确切的错误是什么。
解决方案
推荐阅读
- javascript - React 中的无限渲染
- linux - 为什么管道上的这个 strace 没有完成
- reactjs - 文件阅读器同步
- node.js - Node+React 部署到 Heroku:无法检测到此应用程序的默认语言
- reactjs - 使用 npm 为 react 项目导入 ScrollMagic 插件?
- python-3.x - 如何使用具有管理员属性的 system() 命令?
- python - numpy 从二维矩阵中获取子矩阵
- javascript - 如何在 php 和 javascript 中使用 foreach 更改图像?
- nuget - 如何从 Visual Studio for Mac 中删除 NuGet 包?
- android - 覆盖图片时 RecyclerView 或 Glide 不更新图片