首页 > 解决方案 > 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 不受只有一台创建它的机器的保护。请帮忙,我不知道确切的错误是什么。

标签: c#amazon-s3.net-coredata-protection

解决方案


推荐阅读