Skip to content

Commit 7e094d7

Browse files
authored
Update OIDC SameSite sample (#18934)
1 parent 00e8e95 commit 7e094d7

File tree

1 file changed

+45
-7
lines changed
  • src/Security/Authentication/OpenIdConnect/samples/OpenIdConnectSample

1 file changed

+45
-7
lines changed

src/Security/Authentication/OpenIdConnect/samples/OpenIdConnectSample/Startup.cs

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,58 @@ public Startup(IConfiguration config, IWebHostEnvironment env)
3535

3636
private void CheckSameSite(HttpContext httpContext, CookieOptions options)
3737
{
38-
if (options.SameSite > SameSiteMode.Unspecified)
38+
if (options.SameSite == SameSiteMode.None)
3939
{
40-
var userAgent = httpContext.Request.Headers["User-Agent"];
41-
// TODO: Use your User Agent library of choice here.
42-
if (userAgent.Contains("CPU iPhone OS 12") // Also covers iPod touch
43-
|| userAgent.Contains("iPad; CPU OS 12")
44-
// Safari 12 and 13 are both broken on Mojave
45-
|| userAgent.Contains("Macintosh; Intel Mac OS X 10_14"))
40+
var userAgent = httpContext.Request.Headers["User-Agent"].ToString();
41+
42+
if (DisallowsSameSiteNone(userAgent))
4643
{
4744
options.SameSite = SameSiteMode.Unspecified;
4845
}
4946
}
5047
}
5148

49+
// TODO: Use your User Agent library of choice here.
50+
public static bool DisallowsSameSiteNone(string userAgent)
51+
{
52+
if (string.IsNullOrEmpty(userAgent))
53+
{
54+
return false;
55+
}
56+
57+
// Cover all iOS based browsers here. This includes:
58+
// - Safari on iOS 12 for iPhone, iPod Touch, iPad
59+
// - WkWebview on iOS 12 for iPhone, iPod Touch, iPad
60+
// - Chrome on iOS 12 for iPhone, iPod Touch, iPad
61+
// All of which are broken by SameSite=None, because they use the iOS networking stack
62+
if (userAgent.Contains("CPU iPhone OS 12") || userAgent.Contains("iPad; CPU OS 12"))
63+
{
64+
return true;
65+
}
66+
67+
// Cover Mac OS X based browsers that use the Mac OS networking stack. This includes:
68+
// - Safari on Mac OS X.
69+
// This does not include:
70+
// - Chrome on Mac OS X
71+
// Because they do not use the Mac OS networking stack.
72+
if (userAgent.Contains("Macintosh; Intel Mac OS X 10_14") &&
73+
userAgent.Contains("Version/") && userAgent.Contains("Safari"))
74+
{
75+
return true;
76+
}
77+
78+
// Cover Chrome 50-69, because some versions are broken by SameSite=None,
79+
// and none in this range require it.
80+
// Note: this covers some pre-Chromium Edge versions,
81+
// but pre-Chromium Edge does not require SameSite=None.
82+
if (userAgent.Contains("Chrome/5") || userAgent.Contains("Chrome/6"))
83+
{
84+
return true;
85+
}
86+
87+
return false;
88+
}
89+
5290
public void ConfigureServices(IServiceCollection services)
5391
{
5492
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

0 commit comments

Comments
 (0)