Skip to content

Commit 08b13c0

Browse files
author
Tiago Brenck
committed
HandleSameSiteCookieCompatibility first idea
1 parent d9c62a2 commit 08b13c0

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

1-WebApp-OIDC/1-1-MyOrg/Startup.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public void ConfigureServices(IServiceCollection services)
2626
{
2727
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
2828
options.CheckConsentNeeded = context => true;
29-
options.MinimumSameSitePolicy = SameSiteMode.None;
29+
options.MinimumSameSitePolicy = SameSiteMode.Unspecified;
30+
options.HandleSameSiteCookieCompatibility();
3031
});
3132

3233
// Sign-in users with the Microsoft identity platform

Microsoft.Identity.Web/WebAppServiceCollectionExtensions.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
using Microsoft.AspNetCore.Authentication;
55
using Microsoft.AspNetCore.Authentication.AzureAD.UI;
66
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
7+
using Microsoft.AspNetCore.Builder;
8+
using Microsoft.AspNetCore.Http;
79
using Microsoft.Extensions.Configuration;
810
using Microsoft.Extensions.DependencyInjection;
911
using Microsoft.Extensions.Options;
1012
using Microsoft.Identity.Client;
1113
using Microsoft.Identity.Web.Resource;
1214
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
15+
using System;
1316
using System.Collections.Generic;
1417
using System.Threading.Tasks;
1518

@@ -100,6 +103,7 @@ public static IServiceCollection AddMicrosoftIdentityPlatformAuthentication(
100103
OpenIdConnectMiddlewareDiagnostics.Subscribe(options.Events);
101104
}
102105
});
106+
103107
return services;
104108
}
105109

@@ -158,5 +162,70 @@ public static IServiceCollection AddMsal(this IServiceCollection services, IConf
158162
});
159163
return services;
160164
}
165+
166+
public static CookiePolicyOptions HandleSameSiteCookieCompatibility(this CookiePolicyOptions options)
167+
{
168+
return HandleSameSiteCookieCompatibility(options, DisallowsSameSiteNone);
169+
}
170+
171+
public static CookiePolicyOptions HandleSameSiteCookieCompatibility(this CookiePolicyOptions options, Func<string, bool> disallowsSameSiteNone)
172+
{
173+
options.MinimumSameSitePolicy = SameSiteMode.Unspecified;
174+
options.OnAppendCookie = cookieContext =>
175+
CheckSameSite(cookieContext.Context, cookieContext.CookieOptions, disallowsSameSiteNone);
176+
options.OnDeleteCookie = cookieContext =>
177+
CheckSameSite(cookieContext.Context, cookieContext.CookieOptions, disallowsSameSiteNone);
178+
return options;
179+
}
180+
181+
private static void CheckSameSite(HttpContext httpContext, CookieOptions options, Func<string, bool> disallowsSameSiteNone)
182+
{
183+
if (options.SameSite == SameSiteMode.None)
184+
{
185+
var userAgent = httpContext.Request.Headers["User-Agent"].ToString();
186+
if (disallowsSameSiteNone(userAgent))
187+
{
188+
options.SameSite = SameSiteMode.Unspecified;
189+
}
190+
}
191+
}
192+
193+
public static bool DisallowsSameSiteNone(string userAgent)
194+
{
195+
// Cover all iOS based browsers here. This includes:
196+
// - Safari on iOS 12 for iPhone, iPod Touch, iPad
197+
// - WkWebview on iOS 12 for iPhone, iPod Touch, iPad
198+
// - Chrome on iOS 12 for iPhone, iPod Touch, iPad
199+
// All of which are broken by SameSite=None, because they use the iOS networking
200+
// stack.
201+
if (userAgent.Contains("CPU iPhone OS 12") ||
202+
userAgent.Contains("iPad; CPU OS 12"))
203+
{
204+
return true;
205+
}
206+
207+
// Cover Mac OS X based browsers that use the Mac OS networking stack.
208+
// This includes:
209+
// - Safari on Mac OS X.
210+
// This does not include:
211+
// - Chrome on Mac OS X
212+
// Because they do not use the Mac OS networking stack.
213+
if (userAgent.Contains("Macintosh; Intel Mac OS X 10_14") &&
214+
userAgent.Contains("Version/") && userAgent.Contains("Safari"))
215+
{
216+
return true;
217+
}
218+
219+
// Cover Chrome 50-69, because some versions are broken by SameSite=None,
220+
// and none in this range require it.
221+
// Note: this covers some pre-Chromium Edge versions,
222+
// but pre-Chromium Edge does not require SameSite=None.
223+
if (userAgent.Contains("Chrome/5") || userAgent.Contains("Chrome/6"))
224+
{
225+
return true;
226+
}
227+
228+
return false;
229+
}
161230
}
162231
}

0 commit comments

Comments
 (0)