前幾天微軟的.net core3.1發布后,隨把visual studio 2019升級到16.4.1版本並把項目進行框架升級。升級后的項目在IdentityServer4授權后在360安全瀏覽器竟然無法跳回,測試了demo給的EntityFramework項目,亦是如此,記錄日志如下:
解決方案如下:
在 Startup.cs 中,添加以下代碼 :
private void CheckSameSite(HttpContext httpContext, CookieOptions options) { if (options.SameSite == SameSiteMode.None) { var userAgent = httpContext.Request.Headers["User-Agent"].ToString(); // TODO: Use your User Agent library of choice here. if (/* UserAgent doesn't support new behavior */) { options.SameSite = SameSiteMode.Unspecified; } } } public void ConfigureServices(IServiceCollection services) { services.Configure<CookiePolicyOptions>(options => { options.MinimumSameSitePolicy = SameSiteMode.Unspecified; options.OnAppendCookie = cookieContext => CheckSameSite(cookieContext.Context, cookieContext.CookieOptions); options.OnDeleteCookie = cookieContext => CheckSameSite(cookieContext.Context, cookieContext.CookieOptions); }); } public void Configure(IApplicationBuilder app) { // Before UseAuthentication or anything else that writes cookies. app.UseCookiePolicy(); app.UseAuthentication(); // code omitted for brevity }
“選擇退出”開關
通過 Microsoft.AspNetCore.SuppressSameSiteNone
兼容性開關,可暫時選擇退出新的 ASP.NET Core Cookie 行為。 將以下 JSON 添加到項目的 runtimeconfig.template.json 文件中 :
{ "configProperties": { "Microsoft.AspNetCore.SuppressSameSiteNone": "true" } }
相關文檔:https://docs.microsoft.com/zh-cn/dotnet/core/compatibility/3.0-3.1#support-older-browsers