获取“无法识别的SameSiteMode值-1". ASP.NET Core 3.1 Web应用程序中的InvalidOperationException [英] Getting "Unrecognized SameSiteMode value -1" InvalidOperationException in ASP.NET Core 3.1 Web Application

查看:259
本文介绍了获取“无法识别的SameSiteMode值-1". ASP.NET Core 3.1 Web应用程序中的InvalidOperationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一些测试,以准备对即将到来的Chrome版本进行更改,以对SameSite处理cookie进行更改,但是我的Web应用程序遇到了麻烦.我可以通过以下方式重现它:

I'm running some tests to prepare for the upcoming Chrome version with the changes to SameSite handling of cookies, but my web application is giving trouble. I can reproduce it in the following way:

  1. 使用Visual Studio 2019(16.4.3)创建一个新项目.
  2. 选择"ASP.NET Core Web应用程序"并启用https.
  3. 添加一个脚手架项目"并添加ASP.NET Core身份.
  4. 在被询问时,使用SQLite搭建所有文件,并添加新的数据上下文和用户:

  1. Use Visual Studio 2019 (16.4.3) to create a new project.
  2. Pick "ASP.NET Core Web Application" and enable https.
  3. Add a "Scaffolded Item" and add ASP.NET Core Identity.
  4. When asked, scaffold all files, and add a fresh data context and user, using SQLite:

services.AddRazorPages();添加到启动中

SameSiteCookiesServiceCollectionExtensions添加为ThinkTecture建议的 并通过将services.ConfigureNonBreakingSameSiteCookies();添加到您的启动中来使用它.另外,只需省去浏览器嗅探部分(我认为此复制程序不需要),跳过复制链接的解决方案,而是这样做:

Add the SameSiteCookiesServiceCollectionExtensions as suggested by ThinkTecture and use it by adding services.ConfigureNonBreakingSameSiteCookies(); to your Startup. Alternatively, just leave out the browser sniffing part (not needed for this repro I think), skip copying linked solution, and instead do this:

services.Configure<CookiePolicyOptions>(options =>
{
    options.MinimumSameSitePolicy = (Microsoft.AspNetCore.Http.SameSiteMode)(-1);
});

  • app.UseCookiePolicy();添加到启动中以启用代码

  • Add app.UseCookiePolicy(); to Startup to enable the code

    对我来说,结果是:

    处理请求时发生未处理的异常.
    InvalidOperationException:无法识别的SameSiteMode值-1
    Microsoft.AspNetCore.CookiePolicy.ResponseCookiesWrapper.ApplyPolicy(字符串键,CookieOptions选项)

    An unhandled exception occurred while processing the request.
    InvalidOperationException: Unrecognized SameSiteMode value -1
    Microsoft.AspNetCore.CookiePolicy.ResponseCookiesWrapper.ApplyPolicy(string key, CookieOptions options)

    完整堆栈跟踪很重要:

    InvalidOperationException: Unrecognized SameSiteMode value -1
    Microsoft.AspNetCore.CookiePolicy.ResponseCookiesWrapper.ApplyPolicy(string key, CookieOptions options)
    Microsoft.AspNetCore.CookiePolicy.ResponseCookiesWrapper.ApplyAppendPolicy(ref string key, ref string value, CookieOptions options)
    Microsoft.AspNetCore.CookiePolicy.ResponseCookiesWrapper.Append(string key, string value, CookieOptions options)
    Microsoft.AspNetCore.Authentication.Cookies.ChunkingCookieManager.AppendResponseCookie(HttpContext context, string key, string value, CookieOptions options)
    Microsoft.AspNetCore.Authentication.Cookies.ChunkingCookieManager.DeleteCookie(HttpContext context, string key, CookieOptions options)
    Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler.HandleSignOutAsync(AuthenticationProperties properties)
    Microsoft.AspNetCore.Authentication.AuthenticationService.SignOutAsync(HttpContext context, string scheme, AuthenticationProperties properties)
    AuthTest.Areas.Identity.Pages.Account.LoginModel.OnGetAsync(string returnUrl) in Login.cshtml.cs
    +
                await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
    Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.ExecutorFactory+NonGenericTaskHandlerMethod.Execute(object receiver, object[] arguments)
    Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeHandlerMethodAsync()
    Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeNextPageFilterAsync()
    Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Rethrow(PageHandlerExecutedContext context)
    Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
    Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeInnerFilterAsync()
    Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
    Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
    Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
    Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
    Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
    Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
    Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
    Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
    

    如果我勾选dotnet --info,我会看到:

    If I check dotnet --info I see:

    • Host Version: 3.1.1
    • 安装了
    • .NET Core SDK:最多包括[c11>和3.1.101
    • 多个.NET Core runtimes installed,包括Microsoft.AspNetCore.App版本3.1.03.1.1
    • Host Version: 3.1.1
    • .NET Core SDKs installed: a whole bunch, up to and including 3.1.100 and 3.1.101
    • Several .NET Core runtimes installed including Microsoft.AspNetCore.App versions 3.1.0 and 3.1.1

    一些其他详细信息和尝试的解决方法:

    Some additional details and attempted workarounds:

    • csproj contains <Project Sdk="Microsoft.NET.Sdk.Web"> that should (as far as I know) use the latest stuff available
    • I've rebooted to make sure there is no transient problem lingering
    • one colleague with a Windows machine, can reproduce the problem
    • another colleague, with a Linux Mint machine, does not have the problem
    • I've tried dotnet nuget locals all --clear but it didn't help
    • I can see the error line in the 3.0.0 sources but not in the 3.1.0 sources (or master) of ASP NET Core
    • I have tried dotnet new globaljson --sdk-version 3.1.101 to force the right version, but it didn't work (though I'm not sure if that's the right way to go about using one)
    • I want to try to remove specific versions from my machine but I run into problems uninstalling things (apart from the fact that the Version Selection features should render that approach unnecessary, right?)

    所以我假定我正面临此ASP的变体提到我的版本太低的.NET Core GitHub问题?但是我不确定现在该如何进行,因为我觉得我已经清除了所有内容.

    So I presume I'm facing a variant of this ASP.NET Core GitHub issue that mentions my version is too low? But I'm not sure how to proceed now, since I feel I already cleared everything.

    我在这里想念什么?我该怎么做才能解决此问题?

    What am I missing here? What do I need to do to fix this?

    推荐答案

    如果您不注意并在Visual Studio对话框中选择 3.0.x 来创建应用程序,则会发现这种情况.如果选择正确的一个(3.1.x),您将得到:

    Turns out this happens if you do not pay attention and pick 3.0.x in the Visual Studio dialogs for creating the application. If you pick the right one (3.1.x) you will get:

    <PropertyGroup>
      <TargetFramework>netcoreapp3.1</TargetFramework>
    </PropertyGroup>
    

    文件中的

    ,以及多个软件包的3.1.x版本.

    不知道为什么Linux专门不会出现此问题,也许只是这些机器恰巧没有可用的或未安装3.0.x版本的dotnet core,而是针对3.1运行您的代码,从而掩盖了问题?

    Not sure why Linux would specifically not have the issue, maybe it's just that those machines coincidentally don't have 3.0.x versions of dotnet core available or installed, and it runs your code against 3.1, thus obscuring the problem?

    这篇关于获取“无法识别的SameSiteMode值-1". ASP.NET Core 3.1 Web应用程序中的InvalidOperationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆