c# - asp.net core 2.0 部署 - InvalidOperationException : The antiforgery token could not be decrypted

标签 c# asp.net asp.net-core

最近我在公司开发了一个 asp.net core 2.0 Web 应用程序,在 Debug模式下工作得很好,但是当我将测试服务器部署到 IIS 中并尝试从客户端计算机执行时,它遇到了问题:

An unhandled exception occurred while processing the request.
CryptographicException: The key {0851ad3b-df33-4cf7-8c3a-5c637adaa713} was not found in the key ring.
Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.UnprotectCore(Byte[] protectedData, bool allowOperationsOnRevokedKeys, out UnprotectStatus status)

InvalidOperationException: The antiforgery token could not be decrypted.
Microsoft.AspNetCore.Antiforgery.Internal.DefaultAntiforgeryTokenSerializer.Deserialize(string serializedToken)

当我提交登录页面时,问题就开始了。我调查了此处和其他博客中存在相同问题的链接,但我发现必须使用 ValidateAntiForgeryToken 并且解决方案与 Microsoft.AspNetCore.DataProtection 相关。我将nuget包Microsoft.AspNetCore.DataProtection.Redis添加到我的项目中,并在启动类的ConfigureServices中添加以下代码:

    var redis = ConnectionMultiplexer.Connect("192.168.10.151:80");
    services.AddDataProtection().PersistKeysToRedis(redis, "DataProtection-Keys");
    services.AddOptions();

我们的测试服务器IP是192.168.10.151,但是应用程序抛出以下异常:

RedisConnectionException: It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail. InternalFailure on PING

¿为什么无法连接,因为是在同一个 Web 应用程序服务器中解析的? ¿DataProtection-Keys 数据库位于哪里?

作为解决方法,我使用 PersistKeysToFileSystem 更改了方法,如下所示:

services.AddDataProtection()
                .SetApplicationName("myapp-portal")
                .PersistKeysToFileSystem(new System.IO.DirectoryInfo (@"c:\ProgramData\dpkeys"));

但是,在测试服务器 192.168.10.151 中运行应用程序,提交登录表单后,将返回登录页面。检查标准输出日志文件,仅显示:

托管环境:生产 内容根路径:C:\inetpub\wwwroot\OmniPays 现在收听:http://localhost:30064 申请开始了。按 Ctrl+C 关闭。

通过 Chrome 的开发者工具检查网络消息我注意到了一些事情:

请求网址:http://192.168.10.151/OmniPays/Account/Login 请求方式:POST 状态代码:302 已找到 远程地址:192.168.10.151:80 推荐人政策:降级时无推荐人

然后...

请求网址:http://192.168.10.151/OmniPays/Home/Main 请求方式:GET 状态代码:302 已找到 远程地址:192.168.10.151:80 推荐人政策:降级时无推荐人

仅当身份验证成功时,AccountController 的登录操作才会将请求重定向到 HomeController 的主操作,并且主操作具有 [Authorize] 属性。由于某些原因我无法理解,主要操作失败并返回到登录页面。 Chrome 中的 URL 显示:http://192.168.10.151/OmniPays/Account/Login?ReturnUrl=%2FOmniPays%2FHome%2FMain

我正在使用 Microsoft 身份。在 Debug模式下工作正常,如果我在 IIS 上的本地 PC 上部署应用程序也工作正常。 ¿也许服务器中缺少任何 SDK?

请需要帮助!!

最佳答案

找到解决方案了!问题的原因不在 IIS 中,也不是在服务器中,与服务器的连接使用的是 http 而不是 https,没有涉及验证安全连接的证书,但是在不同服务器应用程序中进行测试工作正常,所以我感到非常失望。解决方案是在所有浏览器中删除 cookie 以及与此 URL 相关的任何指向开发服务器(失败)的数据、之前存储的数据,瞧!!,现在应用程序完美运行。默认情况下,由于 bhmahler 评论数据保护是在内存中进行的,并且我默认保留配置,我的意思是,没有明确地持久化在 redis 中,也没有 PersistKeysToFileSystem 并且工作正常,但是将 DataProtection 设置为强大的数据敏感保护很重要。

我是这些主题的新手,令人难以置信的是,如此简单的事情对我造成了浪费时间。感谢大家!.

关于c# - asp.net core 2.0 部署 - InvalidOperationException : The antiforgery token could not be decrypted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50632347/

相关文章:

ASP.NET 浏览器上限更新

c# - 如何使用 Dapper 从 SQL Server 获取 bigint 到 long 对象属性?

c# - "the term ' 添加迁移 ' is not recognized as the name of a cmdlet" Visual Studio 2019

c# - 从 JWT 验证 User.Identity

c# - 无法将文件上传到 Azure 存储 C#?

C# 扩展方法仅适用于类的实例。有什么办法吗?

c# - 最佳实践/良好编码技术的文献建议

c# - 在以下路径找不到布局页 "~/Views/Shared/"

c# - 异常抛出 : read access violation. 这是 0xBF13D000

asp.net-core - 通过 K8S Ingress 的 SignalR 连接