最初Startup.cs,我们AddDataProtection()完全省略了调用。当我们尝试部署该应用程序时,我们看到了这一点:
System.Security.Cryptography.CryptographicException: The key {...} was not found in the key ring.
at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.UnprotectCore(Byte[] protectedData, Boolean allowOperationsOnRevokedKeys, UnprotectStatus& status)
at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.DangerousUnprotect(Byte[] protectedData, Boolean ignoreRevocationErrors, Boolean& requiresMigration, Boolean& wasRevoked)
at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.Unprotect(Byte[] protectedData)
at Microsoft.AspNetCore.Session.CookieProtection.Unprotect(IDataProtector protector, String protectedText, ILogger logger)
warn: Microsoft.AspNetCore.Session.SessionMiddleware[7]
Error unprotecting the session cookie.
Run Code Online (Sandbox Code Playgroud)
当我添加到 时services.AddData.AddDataProtection().SetApplicationName("MyAppName");,ConfigureServices()我们现在在部署后看到
System.Exception: An error was encountered while handling the remote login. ---> System.Exception: Correlation failed.
--- End of inner exception stack trace ---
at Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler`1.HandleRequestAsync()
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.Invoke(HttpContext context)
warn: Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler[15]
'.AspNetCore.Correlation.OpenIdConnect.{SomeWeirdString}' cookie not found.
Run Code Online (Sandbox Code Playgroud)
我在谷歌上搜索了很多,似乎解决这个问题的“正确”方法是将加密密钥与.PersistKeysToFileSystem(). 有替代方案吗?例如完全禁用加密/数据保护?
对于一些背景,这个应用程序是我们从 ASP.NET 迁移到 ASP.NET Core 的最后阶段的应用程序,之前我们将会话状态存储在 Redis 中,没有加密。
数据保护堆栈是必需的,用于保护 cookie、会话状态、临时数据等中的数据。数据保护在某处保留密钥以加密和解密数据。有多个选项可以保留密钥 - 默认情况下,使用文件系统或注册表。当托管在 IIS 或 Azure 中的本地服务器上时,它通常可以正常工作。对于其他部署选项,尤其是使用容器,您通常必须执行一些配置。例如,容器通常具有临时存储,因此在部署新的容器实例后加密密钥会丢失。
正如您提到的 Redis,我建议您研究一下 Redis 密钥存储提供程序:https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/implementation/key-storage-providers ?view= aspnetcore-2.2&tabs=visual-studio#redis
以下文档资源也值得一读:
| 归档时间: |
|
| 查看次数: |
7519 次 |
| 最近记录: |