11 changed files with 106 additions and 193 deletions
@ -1,163 +1,70 @@ |
|||||
using Microsoft.AspNetCore.Builder; |
using Microsoft.AspNetCore.Builder; |
||||
using Microsoft.AspNetCore.Http; |
using Microsoft.AspNetCore.Http; |
||||
using System; |
|
||||
|
namespace Microsoft.Extensions.DependencyInjection |
||||
/* |
{ |
||||
* See: https://community.abp.io/articles/patch-for-chrome-login-issue-identityserver4-samesite-cookie-problem-weypwp3n
|
public static class SameSiteCookiesServiceCollectionExtensions |
||||
*/ |
{ |
||||
|
public static IServiceCollection AddSameSiteCookiePolicy(this IServiceCollection services) |
||||
namespace Microsoft.Extensions.DependencyInjection |
{ |
||||
{ |
services.Configure<CookiePolicyOptions>(options => |
||||
public static class SameSiteCookiesServiceCollectionExtensions |
{ |
||||
{ |
options.MinimumSameSitePolicy = SameSiteMode.Unspecified; |
||||
/// <summary>
|
options.OnAppendCookie = cookieContext => |
||||
/// -1 defines the unspecified value, which tells ASPNET Core to NOT
|
CheckSameSite(cookieContext.Context, cookieContext.CookieOptions); |
||||
/// send the SameSite attribute. With ASPNET Core 3.1 the
|
options.OnDeleteCookie = cookieContext => |
||||
/// <seealso cref="SameSiteMode" /> enum will have a definition for
|
CheckSameSite(cookieContext.Context, cookieContext.CookieOptions); |
||||
/// Unspecified.
|
}); |
||||
/// </summary>
|
|
||||
private const SameSiteMode Unspecified = (SameSiteMode)(-1); |
return services; |
||||
|
} |
||||
/// <summary>
|
|
||||
/// Configures a cookie policy to properly set the SameSite attribute
|
private static void CheckSameSite(HttpContext httpContext, CookieOptions options) |
||||
/// for Browsers that handle unknown values as Strict. Ensure that you
|
{ |
||||
/// add the <seealso cref="Microsoft.AspNetCore.CookiePolicy.CookiePolicyMiddleware" />
|
if (options.SameSite == SameSiteMode.None) |
||||
/// into the pipeline before sending any cookies!
|
{ |
||||
/// </summary>
|
var userAgent = httpContext.Request.Headers["User-Agent"].ToString(); |
||||
/// <remarks>
|
if (!httpContext.Request.IsHttps || DisallowsSameSiteNone(userAgent)) |
||||
/// Minimum ASPNET Core Version required for this code:
|
{ |
||||
/// - 2.1.14
|
// For .NET Core < 3.1 set SameSite = (SameSiteMode)(-1)
|
||||
/// - 2.2.8
|
options.SameSite = SameSiteMode.Unspecified; |
||||
/// - 3.0.1
|
} |
||||
/// - 3.1.0-preview1
|
} |
||||
/// Starting with version 80 of Chrome (to be released in February 2020)
|
} |
||||
/// cookies with NO SameSite attribute are treated as SameSite=Lax.
|
|
||||
/// In order to always get the cookies send they need to be set to
|
private static bool DisallowsSameSiteNone(string userAgent) |
||||
/// SameSite=None. But since the current standard only defines Lax and
|
{ |
||||
/// Strict as valid values there are some browsers that treat invalid
|
// Cover all iOS based browsers here. This includes:
|
||||
/// values as SameSite=Strict. We therefore need to check the browser
|
// - Safari on iOS 12 for iPhone, iPod Touch, iPad
|
||||
/// and either send SameSite=None or prevent the sending of SameSite=None.
|
// - WkWebview on iOS 12 for iPhone, iPod Touch, iPad
|
||||
/// Relevant links:
|
// - Chrome on iOS 12 for iPhone, iPod Touch, iPad
|
||||
/// - https://tools.ietf.org/html/draft-west-first-party-cookies-07#section-4.1
|
// All of which are broken by SameSite=None, because they use the iOS networking stack
|
||||
/// - https://tools.ietf.org/html/draft-west-cookie-incrementalism-00
|
if (userAgent.Contains("CPU iPhone OS 12") || userAgent.Contains("iPad; CPU OS 12")) |
||||
/// - https://www.chromium.org/updates/same-site
|
{ |
||||
/// - https://devblogs.microsoft.com/aspnet/upcoming-samesite-cookie-changes-in-asp-net-and-asp-net-core/
|
return true; |
||||
/// - https://bugs.webkit.org/show_bug.cgi?id=198181
|
} |
||||
/// </remarks>
|
|
||||
/// <param name="services">The service collection to register <see cref="CookiePolicyOptions" /> into.</param>
|
// Cover Mac OS X based browsers that use the Mac OS networking stack. This includes:
|
||||
/// <returns>The modified <see cref="IServiceCollection" />.</returns>
|
// - Safari on Mac OS X.
|
||||
public static IServiceCollection ConfigureNonBreakingSameSiteCookies(this IServiceCollection services) |
// This does not include:
|
||||
{ |
// - Chrome on Mac OS X
|
||||
services.Configure<CookiePolicyOptions>(options => |
// Because they do not use the Mac OS networking stack.
|
||||
{ |
if (userAgent.Contains("Macintosh; Intel Mac OS X 10_14") && |
||||
options.MinimumSameSitePolicy = Unspecified; |
userAgent.Contains("Version/") && userAgent.Contains("Safari")) |
||||
options.OnAppendCookie = cookieContext => |
{ |
||||
CheckSameSite(cookieContext.Context, cookieContext.CookieOptions); |
return true; |
||||
options.OnDeleteCookie = cookieContext => |
} |
||||
CheckSameSite(cookieContext.Context, cookieContext.CookieOptions); |
|
||||
}); |
// Cover Chrome 50-69, because some versions are broken by SameSite=None,
|
||||
|
// and none in this range require it.
|
||||
return services; |
// Note: this covers some pre-Chromium Edge versions,
|
||||
} |
// but pre-Chromium Edge does not require SameSite=None.
|
||||
|
if (userAgent.Contains("Chrome/5") || userAgent.Contains("Chrome/6")) |
||||
private static void CheckSameSite(HttpContext httpContext, CookieOptions options) |
{ |
||||
{ |
return true; |
||||
if (options.SameSite == SameSiteMode.None) |
} |
||||
{ |
|
||||
var userAgent = httpContext.Request.Headers["User-Agent"].ToString(); |
return false; |
||||
|
} |
||||
if (DisallowsSameSiteNone(userAgent)) |
} |
||||
{ |
|
||||
options.SameSite = Unspecified; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Checks if the UserAgent is known to interpret an unknown value as Strict.
|
|
||||
/// For those the <see cref="CookieOptions.SameSite" /> property should be
|
|
||||
/// set to <see cref="Unspecified" />.
|
|
||||
/// </summary>
|
|
||||
/// <remarks>
|
|
||||
/// This code is taken from Microsoft:
|
|
||||
/// https://devblogs.microsoft.com/aspnet/upcoming-samesite-cookie-changes-in-asp-net-and-asp-net-core/
|
|
||||
/// </remarks>
|
|
||||
/// <param name="userAgent">The user agent string to check.</param>
|
|
||||
/// <returns>Whether the specified user agent (browser) accepts SameSite=None or not.</returns>
|
|
||||
private static bool DisallowsSameSiteNone(string userAgent) |
|
||||
{ |
|
||||
// Cover all iOS based browsers here. This includes:
|
|
||||
// - Safari on iOS 12 for iPhone, iPod Touch, iPad
|
|
||||
// - WkWebview on iOS 12 for iPhone, iPod Touch, iPad
|
|
||||
// - Chrome on iOS 12 for iPhone, iPod Touch, iPad
|
|
||||
// All of which are broken by SameSite=None, because they use the
|
|
||||
// iOS networking stack.
|
|
||||
// Notes from Thinktecture:
|
|
||||
// Regarding https://caniuse.com/#search=samesite iOS versions lower
|
|
||||
// than 12 are not supporting SameSite at all. Starting with version 13
|
|
||||
// unknown values are NOT treated as strict anymore. Therefore we only
|
|
||||
// need to check version 12.
|
|
||||
if (userAgent.Contains("CPU iPhone OS 12") |
|
||||
|| userAgent.Contains("iPad; CPU OS 12")) |
|
||||
{ |
|
||||
return true; |
|
||||
} |
|
||||
|
|
||||
// Cover Mac OS X based browsers that use the Mac OS networking stack.
|
|
||||
// This includes:
|
|
||||
// - Safari on Mac OS X.
|
|
||||
// This does not include:
|
|
||||
// - Chrome on Mac OS X
|
|
||||
// because they do not use the Mac OS networking stack.
|
|
||||
// Notes from Thinktecture:
|
|
||||
// Regarding https://caniuse.com/#search=samesite MacOS X versions lower
|
|
||||
// than 10.14 are not supporting SameSite at all. Starting with version
|
|
||||
// 10.15 unknown values are NOT treated as strict anymore. Therefore we
|
|
||||
// only need to check version 10.14.
|
|
||||
if (userAgent.Contains("Safari") |
|
||||
&& userAgent.Contains("Macintosh; Intel Mac OS X 10_14") |
|
||||
&& userAgent.Contains("Version/")) |
|
||||
{ |
|
||||
return true; |
|
||||
} |
|
||||
|
|
||||
// Cover Chrome 50-69, because some versions are broken by SameSite=None
|
|
||||
// and none in this range require it.
|
|
||||
// Note: this covers some pre-Chromium Edge versions,
|
|
||||
// but pre-Chromium Edge does not require SameSite=None.
|
|
||||
// Notes from Thinktecture:
|
|
||||
// We can not validate this assumption, but we trust Microsofts
|
|
||||
// evaluation. And overall not sending a SameSite value equals to the same
|
|
||||
// behavior as SameSite=None for these old versions anyways.
|
|
||||
if (userAgent.Contains("Chrome/5") || userAgent.Contains("Chrome/6")) |
|
||||
{ |
|
||||
return true; |
|
||||
} |
|
||||
|
|
||||
if (GetChromeVersion(userAgent) >= 80) |
|
||||
{ |
|
||||
return true; |
|
||||
} |
|
||||
|
|
||||
return false; |
|
||||
} |
|
||||
|
|
||||
private static int GetChromeVersion(string userAgent) |
|
||||
{ |
|
||||
try |
|
||||
{ |
|
||||
string version = "0"; |
|
||||
var chromeAgents = userAgent.Split("Chrome/"); |
|
||||
if (chromeAgents.Length > 1 && chromeAgents[1].Split('.').Length > 0) |
|
||||
{ |
|
||||
version = chromeAgents[1].Split('.')[0]; |
|
||||
} |
|
||||
return Convert.ToInt32(version); |
|
||||
} |
|
||||
catch (Exception) |
|
||||
{ |
|
||||
return 0; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
} |
||||
Loading…
Reference in new issue