[英]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:
我可以通过以下方式重现它:
When asked, scaffold all files, and add a fresh data context and user, using SQLite:当被问到时,使用 SQLite 搭建所有文件,并添加一个新的数据上下文和用户:
Add services.AddRazorPages();
添加
services.AddRazorPages();
to Startup启动
endpoints.MapRazorPages();
endpoints.MapRazorPages();
into the UseEndpoints
configuration lambdaUseEndpoints
配置 lambda 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); });
Add app.UseCookiePolicy();
添加
app.UseCookiePolicy();
to Startup to enable the code启动以启用代码
https://localhost:44342/
)https://localhost:44342/
上的 IIS Express 中启动)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 -1InvalidOperationException:无法识别的 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
3.1.100
and 3.1.101
3.1.100
和3.1.101
.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.0
和3.1.1
Some additional details and attempted workarounds:一些额外的细节和尝试的解决方法:
<Project Sdk="Microsoft.NET.Sdk.Web">
that should (as far as I know) use the latest stuff available <Project Sdk="Microsoft.NET.Sdk.Web">
应该(据我所知)使用最新可用的东西dotnet nuget locals all --clear
but it didn't helpdotnet nuget locals all --clear
但它没有帮助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
来强制使用正确的版本,但它没有用(虽然我不确定这是否是使用一个的正确方法) 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.