Skip to content

Commit 0c88411

Browse files
committed
Sync changes
1 parent c6016a6 commit 0c88411

File tree

5 files changed

+30
-2
lines changed

5 files changed

+30
-2
lines changed

src/Http/Headers/src/SetCookieHeaderValue.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ public override string ToString()
166166
length += SeparatorToken.Length + HttpOnlyToken.Length;
167167
}
168168

169-
return string.Create(length, (this, maxAge), (span, tuple) =>
169+
return string.Create(length, (this, maxAge, sameSite), (span, tuple) =>
170170
{
171-
var (headerValue, maxAgeValue) = tuple;
171+
var (headerValue, maxAgeValue, sameSite) = tuple;
172172

173173
Append(ref span, headerValue._name);
174174
Append(ref span, EqualsToken);

src/Security/Authentication/OpenIdConnect/samples/OpenIdConnectSample/OpenIdConnectSample.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
<Reference Include="Microsoft.AspNetCore" />
1515
<Reference Include="Microsoft.AspNetCore.Authentication.Cookies" />
1616
<Reference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" />
17+
<Reference Include="Microsoft.AspNetCore.CookiePolicy" />
1718
<Reference Include="Microsoft.Extensions.FileProviders.Embedded" />
19+
<Reference Include="Microsoft.Net.Http.Headers" />
1820
</ItemGroup>
1921

2022
<ItemGroup>

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,33 @@ public Startup(IConfiguration config, IWebHostEnvironment env)
3232
public IConfiguration Configuration { get; set; }
3333
public IWebHostEnvironment Environment { get; }
3434

35+
private void CheckSameSite(HttpContext httpContext, CookieOptions options)
36+
{
37+
if (options.SameSite > (SameSiteMode)(-1))
38+
{
39+
var userAgent = httpContext.Request.Headers["User-Agent"].ToString();
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)(-1);
47+
}
48+
}
49+
}
50+
3551
public void ConfigureServices(IServiceCollection services)
3652
{
3753
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
3854

55+
services.Configure<CookiePolicyOptions>(options =>
56+
{
57+
options.MinimumSameSitePolicy = (SameSiteMode)(-1);
58+
options.OnAppendCookie = cookieContext => CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
59+
options.OnDeleteCookie = cookieContext => CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
60+
});
61+
3962
services.AddAuthentication(sharedOptions =>
4063
{
4164
sharedOptions.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
@@ -84,6 +107,7 @@ public void ConfigureServices(IServiceCollection services)
84107
public void Configure(IApplicationBuilder app, IOptionsMonitor<OpenIdConnectOptions> optionsMonitor)
85108
{
86109
app.UseDeveloperExceptionPage();
110+
app.UseCookiePolicy(); // Before UseAuthentication or anything else that writes cookies.
87111
app.UseAuthentication();
88112

89113
app.Run(async context =>

src/Security/Authentication/test/Microsoft.AspNetCore.Authentication.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<Reference Include="Microsoft.AspNetCore.Authentication.WsFederation" />
5050
<Reference Include="Microsoft.AspNetCore.HttpOverrides" />
5151
<Reference Include="Microsoft.AspNetCore.TestHost" />
52+
<Reference Include="Microsoft.Net.Http.Headers" />
5253
</ItemGroup>
5354

5455
</Project>

src/Security/CookiePolicy/test/Microsoft.AspNetCore.CookiePolicy.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<Reference Include="Microsoft.AspNetCore.CookiePolicy" />
1414
<Reference Include="Microsoft.AspNetCore.TestHost" />
1515
<Reference Include="Microsoft.Extensions.DependencyInjection" />
16+
<Reference Include="Microsoft.Net.Http.Headers" />
1617
</ItemGroup>
1718

1819
</Project>

0 commit comments

Comments
 (0)