|
4 | 4 | using Microsoft.AspNetCore.Authentication;
|
5 | 5 | using Microsoft.AspNetCore.Authentication.AzureAD.UI;
|
6 | 6 | using Microsoft.AspNetCore.Authentication.OpenIdConnect;
|
| 7 | +using Microsoft.AspNetCore.Builder; |
| 8 | +using Microsoft.AspNetCore.Http; |
7 | 9 | using Microsoft.Extensions.Configuration;
|
8 | 10 | using Microsoft.Extensions.DependencyInjection;
|
9 | 11 | using Microsoft.Extensions.Options;
|
10 | 12 | using Microsoft.Identity.Client;
|
11 | 13 | using Microsoft.Identity.Web.Resource;
|
12 | 14 | using Microsoft.IdentityModel.Protocols.OpenIdConnect;
|
| 15 | +using System; |
13 | 16 | using System.Collections.Generic;
|
14 | 17 | using System.Threading.Tasks;
|
15 | 18 |
|
@@ -100,6 +103,7 @@ public static IServiceCollection AddMicrosoftIdentityPlatformAuthentication(
|
100 | 103 | OpenIdConnectMiddlewareDiagnostics.Subscribe(options.Events);
|
101 | 104 | }
|
102 | 105 | });
|
| 106 | + |
103 | 107 | return services;
|
104 | 108 | }
|
105 | 109 |
|
@@ -158,5 +162,70 @@ public static IServiceCollection AddMsal(this IServiceCollection services, IConf
|
158 | 162 | });
|
159 | 163 | return services;
|
160 | 164 | }
|
| 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 | + } |
161 | 230 | }
|
162 | 231 | }
|
0 commit comments