@@ -32,14 +32,36 @@ public Startup(IConfiguration config, IWebHostEnvironment env)
32
32
public IConfiguration Configuration { get ; set ; }
33
33
public IWebHostEnvironment Environment { get ; }
34
34
35
+ private void CheckSameSite ( HttpContext httpContext , CookieOptions options )
36
+ {
37
+ if ( options . SameSite > SameSiteMode . Unspecified )
38
+ {
39
+ var userAgent = httpContext . Request . Headers [ "User-Agent" ] ;
40
+ // TODO: Use your User Agent library of choice here.
41
+ if ( userAgent . Contains ( "CPU iPhone OS 12" ) // Also covers iPod touch
42
+ || userAgent . Contains ( "iPad; CPU OS 12" )
43
+ // Safari 12 and 13 are both broken on Mojave
44
+ || userAgent . Contains ( "Macintosh; Intel Mac OS X 10_14" ) )
45
+ {
46
+ options . SameSite = SameSiteMode . Unspecified ;
47
+ }
48
+ }
49
+ }
50
+
35
51
public void ConfigureServices ( IServiceCollection services )
36
52
{
37
53
JwtSecurityTokenHandler . DefaultInboundClaimTypeMap . Clear ( ) ;
38
54
55
+ services . Configure < CookiePolicyOptions > ( options =>
56
+ {
57
+ options . MinimumSameSitePolicy = SameSiteMode . Unspecified ;
58
+ options . OnAppendCookie = cookieContext => CheckSameSite ( cookieContext . Context , cookieContext . CookieOptions ) ;
59
+ options . OnDeleteCookie = cookieContext => CheckSameSite ( cookieContext . Context , cookieContext . CookieOptions ) ;
60
+ } ) ;
61
+
39
62
services . AddAuthentication ( sharedOptions =>
40
63
{
41
- sharedOptions . DefaultAuthenticateScheme = CookieAuthenticationDefaults . AuthenticationScheme ;
42
- sharedOptions . DefaultSignInScheme = CookieAuthenticationDefaults . AuthenticationScheme ;
64
+ sharedOptions . DefaultScheme = CookieAuthenticationDefaults . AuthenticationScheme ;
43
65
sharedOptions . DefaultChallengeScheme = OpenIdConnectDefaults . AuthenticationScheme ;
44
66
} )
45
67
. AddCookie ( )
@@ -84,6 +106,7 @@ public void ConfigureServices(IServiceCollection services)
84
106
public void Configure ( IApplicationBuilder app , IOptionsMonitor < OpenIdConnectOptions > optionsMonitor )
85
107
{
86
108
app . UseDeveloperExceptionPage ( ) ;
109
+ app . UseCookiePolicy ( ) ; // Before UseAuthentication or anything else that writes cookies.
87
110
app . UseAuthentication ( ) ;
88
111
89
112
app . Run ( async context =>
0 commit comments