首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“取消保护会话cookie时出错”异常

“取消保护会话cookie时出错”异常
EN

Stack Overflow用户
提问于 2016-11-29 19:39:22
回答 2查看 13.7K关注 0票数 12

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

ConfigureServices():

代码语言:javascript
复制
services.AddSession()
services.AddAuthentication(sharedOptions => sharedOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme);

Configure():

代码语言:javascript
复制
        app.UseCookieAuthentication(new CookieAuthenticationOptions());
        app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
        {
            ClientId = "xx",
            Authority = "xx",
            Events = new OpenIdConnectEvents { OnRemoteFailure = this.OnAuthenticationFailed }
        });

当托管在IIS中时,一些用户会收到以下异常:

代码语言:javascript
复制
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上运行了此程序

网站只有HTTPS绑定,SSL证书可以并签名。导致此问题的原因是什么?这个"key“值到底是什么?

EN

回答 2

Stack Overflow用户

发布于 2021-08-30 07:44:09

我也有同样的问题。我通过以下方式修复它:

如下所示的

Startup的ConfigureServices方法:

代码语言:javascript
复制
    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;
    });

启动的配置方法:

代码语言:javascript
复制
        app.UseCookiePolicy();

        app.UseSession();

在浏览器中为此网站

  • Deleting所有现有的cookies (或者服务器可能会尝试读取旧的cookies,即使您同时修复了问题)
票数 -1
EN

Stack Overflow用户

发布于 2019-10-10 13:12:57

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

代码语言:javascript
复制
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;
    });

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

票数 -3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40865011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档