简体   繁体   English

在 ASP.NET Core 3.1 Web 应用程序中获取“无法识别的 SameSiteMode 值 -1”InvalidOperationException

[英]Getting "Unrecognized SameSiteMode value -1" InvalidOperationException in ASP.NET Core 3.1 Web Application

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.我正在运行一些测试,为即将到来的 Chrome 版本做准备,其中包含对 SameSite cookie 处理的更改,但我的 Web 应用程序出现了问题。 I can reproduce it in the following way:我可以通过以下方式重现它:

  1. Use Visual Studio 2019 (16.4.3) to create a new project.使用 Visual Studio 2019 (16.4.3) 创建新项目。
  2. Pick "ASP.NET Core Web Application" and enable https.选择“ASP.NET Core Web 应用程序”并启用 https。
  3. Add a "Scaffolded Item" and add ASP.NET Core Identity.添加“脚手架项”并添加 ASP.NET Core 标识。
  4. When asked, scaffold all files, and add a fresh data context and user, using SQLite:当被问到时,使用 SQLite 搭建所有文件,并添加一个新的数据上下文和用户:

    对话框中的第 4 步

  5. Add services.AddRazorPages();添加services.AddRazorPages(); to Startup启动

  6. Add endpoints.MapRazorPages();添加endpoints.MapRazorPages(); into the UseEndpoints configuration lambda进入UseEndpoints配置 lambda
  7. Add the SameSiteCookiesServiceCollectionExtensions as suggested by ThinkTecture and use it by adding services.ConfigureNonBreakingSameSiteCookies();按照SameSiteCookiesServiceCollectionExtensions 建议添加SameSiteCookiesServiceCollectionExtensions并通过添加services.ConfigureNonBreakingSameSiteCookies();使用它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); });
  8. Add app.UseCookiePolicy();添加app.UseCookiePolicy(); to Startup to enable the code启动以启用代码

  9. Start the application (for me it starts in IIS Express on https://localhost:44342/ )启动应用程序(对我来说它在https://localhost:44342/上的 IIS Express 中启动)
  10. Navigate to https://localhost:44342/Identity/Account/Login导航到https://localhost:44342/Identity/Account/Login

The result for me is:对我来说结果是:

An unhandled exception occurred while processing the request.处理请求时发生未处理的异常。
InvalidOperationException: Unrecognized SameSiteMode value -1 InvalidOperationException:无法识别的 SameSiteMode 值 -1
Microsoft.AspNetCore.CookiePolicy.ResponseCookiesWrapper.ApplyPolicy(string key, CookieOptions options) Microsoft.AspNetCore.CookiePolicy.ResponseCookiesWrapper.ApplyPolicy(字符串键,CookieOptions 选项)

The full stack trace, should it be important:完整的堆栈跟踪,是否重要:

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)

If I check dotnet --info I see:如果我检查dotnet --info我看到:

  • Host Version: 3.1.1
  • .NET Core SDKs installed: a whole bunch, up to and including 3.1.100 and 3.1.101安装的 .NET Core SDK:一大堆,包括3.1.1003.1.101
  • Several .NET Core runtimes installed including Microsoft.AspNetCore.App versions 3.1.0 and 3.1.1 .NET Core runtimes installed多个.NET Core runtimes installed包括Microsoft.AspNetCore.App版本3.1.03.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 csproj 包含<Project Sdk="Microsoft.NET.Sdk.Web">应该(据我所知)使用最新可用的东西
  • I've rebooted to make sure there is no transient problem lingering我已重新启动以确保没有暂时性问题挥之不去
  • one colleague with a Windows machine, can reproduce the problem一位拥有 Windows 机器的同事,可以重现该问题
  • another colleague, with a Linux Mint machine, does not have the problem另一位同事,与Linux Mint的机器,不会有问题
  • I've tried dotnet nuget locals all --clear but it didn't help我已经尝试过dotnet nuget locals all --clear但它没有帮助
  • 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我可以在 3.0.0 源中看到错误行ASP NET Core 的 3.1.0 源(或主)中看不到
  • 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)我已经尝试过dotnet new globaljson --sdk-version 3.1.101来强制使用正确的版本,但它没有用(虽然我不确定这是否是使用一个的正确方法)
  • 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?)我想尝试从我的机​​器中删除特定版本,但我在卸载东西时遇到了问题(除了版本选择功能应该使该方法变得不必要的事实,对吧?)

So I presume I'm facing a variant of this ASP.NET Core GitHub issue that mentions my version is too low?所以我相信我现在面临的一个变种这个ASP.NET核心GitHub的问题是提到我的版本太低了? 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?我需要做什么来解决这个问题?

Turns out this happens if you do not pay attention and pick 3.0.x in the Visual Studio dialogs for creating the application.事实证明,如果您不注意并在 Visual Studio 对话框中选择3.0.x来创建应用程序,就会发生这种情况。 If you pick the right one (3.1.x) you will get:如果您选择正确的 (3.1.x),您将获得:

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

in your .csproj file, as well as 3.1.x versions of several packages instead.在您的.csproj文件中,以及几个包的3.1.x版本。

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?不知道为什么 Linux 不会特别有问题,也许只是那些机器巧合地没有 3.0.x 版本的 dotnet core 可用或安装,并且它针对 3.1 运行您的代码,从而掩盖了问题?

Many possibilities have been tried to try.已经尝试了许多可能性。 But the solution is this: You have to install this.但解决方案是这样的:你必须安装它。 ASP.NET Core Runtime 2.1.17 ASP.NET 核心运行时 2.1.17

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 asp.net 核心 3.1 web 应用程序中未显示的代码参考 - References of code not showing in asp.net core 3.1 web application ASP.NET Web应用程序的WCF服务中的InvalidOperationException - InvalidOperationException in wcf service of asp.net web application 如何在 asp.net 核心 3.1 mvc web 应用程序中将日志保存到数据库? - How do I save logs to database in asp.net core 3.1 mvc web application? 我应该如何保护我的 Web 应用程序(ASP.Net Core 3.1 MVC)? - How should i secure my Web Application(ASP.Net Core 3.1 MVC)? ASP.NET Core 3.1 Web 应用程序失败,出现“Microsoft.NETCore.App”,找不到版本“3.1.27”错误 - ASP.NET Core 3.1 Web application failed with 'Microsoft.NETCore.App', version '3.1.27' was not found error Serving and Verifying.network 在 Asp.Net Core 3.1 中共享图像 Web 应用程序 (Razor Pages) - Serving and Verifying network share images in Asp.Net Core 3.1 Web Application (Razor Pages) 具有 InProcess 托管模型的 ASP.NET Core 3.1 应用程序在 1 分钟后超时 - ASP.NET Core 3.1 application having InProcess hosting model is getting timeout after 1 minute 是否可以将 ASP.Net Core 3.1 Web 应用程序发布为单个文件 - Is that possible to publish ASP.Net Core 3.1 web application as a Single File 如何在 ASP.NET Core 3.1 Web API 中将键值从结果转换为大写 - How to convert key value from result to uppercase in an ASP.NET Core 3.1 Web API ASP.NET Core 3.1:Web API 身份登录 - ASP.NET Core 3.1: Web API identity sign in
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM