使用chrome最新80的版本在登录后始终跳回到登录页面。

    1. public static class SameSiteCookiesServiceCollectionExtensions
    2. {
    3. public static IServiceCollection AddSameSiteCookiePolicy(this IServiceCollection services)
    4. {
    5. services.Configure<CookiePolicyOptions>(options =>
    6. {
    7. options.MinimumSameSitePolicy = SameSiteMode.Unspecified;
    8. options.OnAppendCookie = cookieContext =>
    9. CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
    10. options.OnDeleteCookie = cookieContext =>
    11. CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
    12. });
    13. return services;
    14. }
    15. private static void CheckSameSite(HttpContext httpContext, CookieOptions options)
    16. {
    17. if (options.SameSite == SameSiteMode.None)
    18. {
    19. var userAgent = httpContext.Request.Headers["User-Agent"].ToString();
    20. if (!httpContext.Request.IsHttps || DisallowsSameSiteNone(userAgent))
    21. {
    22. // For .NET Core < 3.1 set SameSite = (SameSiteMode)(-1)
    23. options.SameSite = SameSiteMode.Unspecified;
    24. }
    25. }
    26. }
    27. private static bool DisallowsSameSiteNone(string userAgent)
    28. {
    29. // Cover all iOS based browsers here. This includes:
    30. // - Safari on iOS 12 for iPhone, iPod Touch, iPad
    31. // - WkWebview on iOS 12 for iPhone, iPod Touch, iPad
    32. // - Chrome on iOS 12 for iPhone, iPod Touch, iPad
    33. // All of which are broken by SameSite=None, because they use the iOS networking stack
    34. if (userAgent.Contains("CPU iPhone OS 12") || userAgent.Contains("iPad; CPU OS 12"))
    35. {
    36. return true;
    37. }
    38. // Cover Mac OS X based browsers that use the Mac OS networking stack. This includes:
    39. // - Safari on Mac OS X.
    40. // This does not include:
    41. // - Chrome on Mac OS X
    42. // Because they do not use the Mac OS networking stack.
    43. if (userAgent.Contains("Macintosh; Intel Mac OS X 10_14") &&
    44. userAgent.Contains("Version/") && userAgent.Contains("Safari"))
    45. {
    46. return true;
    47. }
    48. // Cover Chrome 50-69, because some versions are broken by SameSite=None,
    49. // and none in this range require it.
    50. // Note: this covers some pre-Chromium Edge versions,
    51. // but pre-Chromium Edge does not require SameSite=None.
    52. if (userAgent.Contains("Chrome/5") || userAgent.Contains("Chrome/6"))
    53. {
    54. return true;
    55. }
    56. return false;
    57. }
    58. }