【问题标题】:An exception was thrown while deserializing the token.The antiforgery token could not be decrypted in .Net Core 2.2 application反序列化令牌时引发异常。无法在 .Net Core 2.2 应用程序中解密防伪令牌
【发布时间】:2020-12-06 04:36:50
【问题描述】:

我的日志中出现错误。我花了大部分时间寻找解决方案,但找不到符合我要求的解决方案。

这是日志错误

严重性=[错误],ipaddress=xxxx, 子进程=Microsoft.AspNetCore.Antiforgery.Internal.DefaultAntiforgery, 描述=反序列化令牌时引发异常。 Microsoft.AspNetCore.Antiforgery.AntiforgeryValidationException: 防伪令牌无法解密。 ---> System.Security.Cryptography.CryptographicException:密钥 未在密钥环中找到 {xxxxxxxxx}。在 Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.UnprotectCore(字节[] protectedData, Boolean allowOperationsOnRevokedKeys, UnprotectStatus& 状态)在 Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.DangerousUnprotect(字节[] protectedData,布尔型 ignoreRevocationErrors,布尔型& requiresMigration, Boolean& wasRevoked) 在 Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.Unprotect(字节[] 受保护数据)在 Microsoft.AspNetCore.Antiforgery.Internal.DefaultAntiforgeryTokenSerializer.Deserialize(字符串 序列化令牌)在 Microsoft.AspNetCore.Antiforgery.Internal.DefaultAntiforgeryTokenSerializer.Deserialize(字符串 序列化令牌)在 Microsoft.AspNetCore.Antiforgery.Internal.DefaultAntiforgery.GetCookieTokenDoesNotThrow(HttpContext httpContext)

    "Certificates": {
    "StoreName": "My",
    "StoreLocation": "LocalMachine"
    "SerialNumber": "xxxxxxxxxxxx"
},
   
   private X509Certificate2 LCertificate()
    {
        var storeName = Configuration["Certificates:StoreName"];
        var storeLocation = Configuration["Certificates:StoreLocation"];
        string serialNumber = Configuration["Certificates: SerialNumber"];
        using(X509Store store = new X509Store(storeName,storeLocation))
        {
            var certificates = store.Certificates
                                    .Find(X509FindType.FindBySerialNumber,
                                          serialNumber,
                                          acceptValidCertOnly);             

            return certificates[0];
        }
    }
    
     public void ConfigureServices(IServiceCollection services)
    {
        services.AddIdentityServer
                .AddSigningCredential(new X509Certificate2(LCertificate()))
      
    }

   [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Login(LoginModel model)
    {

【问题讨论】:

    标签: c# asp.net-core antiforgerytoken


    【解决方案1】:

    如果

    • 您的应用托管在多台服务器上
    • 尚未配置共享数据保护
    • 您没有使用粘性会话

    当用户从服务器 A 请求带有表单的页面,然后将表单提交到服务器 B 时,就会发生这种情况。

    它也可能发生在单个 IIS 服务器上,如果

    • 用户请求带有表单的页面
    • 你重启服务器
    • 用户提交表单

    原因是重新启动会导致新的密钥环加载到内存中,并且表单内的防伪密钥不再有效。

    后一种情况可以通过检查应用程序池中的“加载用户配置文件”在 IIS 中修复。

    更多信息: https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/configuration/overview?view=aspnetcore-3.1

    【讨论】:

    • 就我而言,我同时使用单个 IIS 服务器和多个服务器。我还在我的应用程序中使用粘性会话。另外,我还没有重新启动服务器,但这个问题是间歇性的。有时我会遇到问题,有时不会。请提供两种情况的解决方案。谢谢
    • 此解决方案是否适用于单个服务器和共享服务器?如果我只是更改load user profile 怎么办?在不创建数据库和编写这么多代码的情况下有什么办法吗?请指导我。谢谢
    • 对于运行 IIS 的单个服务器,加载用户配置文件就足够了。
    • 如果您将“蓝绿”部署到两个不同的文件夹以实现“零停机时间”部署(然后将 IIS 从一个映射到另一个),请确保您还设置了明确的应用程序名称: services.AddDataProtection().SetApplicationName("shared app name");
    猜你喜欢
    • 2020-04-09
    • 1970-01-01
    • 2017-07-21
    • 2023-03-30
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多