asp.net core 2.0部署 - InvalidOperationException:无法解密防伪令牌

时间:2018-05-31 20:31:55

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

最近我在我的公司开发了一个asp.net core 2.0 web应用程序,并且在调试模式下运行完美,但是当我在我们的测试服务器中部署到IIS并且我们尝试从客户端机器执行时遇到了一个问题:< / p>

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,但app会抛出以下异常:

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中运行应用程序时,提交登录表单时,返回登录页面。检查stdout日志文件,只显示:

托管环境:生产 内容根路径: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 推荐人政策:no-referrer-when-downgrade

然后......

请求网址:http://192.168.10.151/OmniPays/Home/Main 请求方法:GET 状态代码:302找到 远程地址:192.168.10.151:80 推荐人政策:no-referrer-when-downgrade

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

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

请帮助!!

1 个答案:

答案 0 :(得分:2)

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

我是关于这些主题的新手,令人难以置信的是这样一件简单的事情对我造成浪费时间。谢谢大家!。