【问题标题】:"Error unprotecting the session cookie" exception“取消保护会话 cookie 时出错”异常
【发布时间】:2016-11-29 11:39:21
【问题描述】:

我有一个带有此身份验证设置的 Asp.NET MVC 应用程序:

配置服务():

services.AddSession()
services.AddAuthentication(sharedOptions => sharedOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme);

配置():

        app.UseCookieAuthentication(new CookieAuthenticationOptions());
        app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
        {
            ClientId = "xx",
            Authority = "xx",
            Events = new OpenIdConnectEvents { OnRemoteFailure = this.OnAuthenticationFailed }
        });

在 IIS 中托管时,某些用户会遇到此异常:

Microsoft.AspNetCore.Session.SessionMiddleware, 
      Error unprotecting the session cookie.
System.Security.Cryptography.CryptographicException: The key {9ec59def-874e-45df-9bac-d629f5716a04} 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)

我已经在托管服务器https://github.com/aspnet/DataProtection/blob/dev/Provision-AutoGenKeys.ps1上运行了这个

Web 仅具有 HTTPS 绑定,SSL 证书正常且已签名。什么可能导致这个问题?这个“关键”值究竟是什么?

【问题讨论】:

  • 您是否在负载均衡器后面的多个实例中运行代码?此问题是否仅在应用程序重新启动之间发生?
  • @RubbleFord 没有负载均衡器,没有 webfarm。一台服务器,一台 IIS,一个服务所有请求的站点。
  • 我也看到很多这样的:警告:Microsoft.AspNetCore.Session.DistributedSession[2] Accessing expired session, Key:67a44622-cea8-dd31-b0af-5b164cbec2ca
  • 您解决了这个问题吗?我认为我遇到了类似的问题。
  • 我的问题是我没有配置基于 Cookie 的 TempData 提供程序。见:docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state

标签: authentication iis asp.net-core


【解决方案1】:

我遇到了同样的问题。 我通过以下方式修复了它:

Startup 的 ConfigureServices 方法:

    services.AddControllersWithViews()
            .AddSessionStateTempDataProvider();

    services.AddRazorPages()
            .AddSessionStateTempDataProvider();

    services.AddSession(options =>
    {
        options.IdleTimeout = TimeSpan.FromHours(4);
        options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
        options.Cookie.SameSite = SameSiteMode.Strict;
        options.Cookie.HttpOnly = true;
        // Make the session cookie essential if you wish
        //options.Cookie.IsEssential = true;
    });

Startup 的配置方法:

        app.UseCookiePolicy();

        app.UseSession();
  • 删除本网站浏览器中的所有现有 cookie(或者服务器可能会尝试读取旧 cookie,即使您同时解决了问题)

【讨论】:

    【解决方案2】:

    将您的 services.AddSession() 更改为以下内容:

    services.AddSession(options =>
        {
            // Set a short timeout for easy testing.
            options.IdleTimeout = TimeSpan.FromMinutes(60);
            // You might want to only set the application cookies over a secure connection:
            options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
            options.Cookie.SameSite = SameSiteMode.Strict;
            options.Cookie.HttpOnly = true;
            // Make the session cookie essential
            options.Cookie.IsEssential = true;
        });
    

    这应该可以解决您的问题!

    【讨论】:

    • 为什么这样可以解决问题?出了什么问题?
    猜你喜欢
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-30
    • 2011-05-27
    • 1970-01-01
    • 2012-02-18
    • 2015-08-15
    相关资源
    最近更新 更多