Skip to content

Commit 3e34315

Browse files
committed
Add user agent sniffing sample
1 parent 3b08fa3 commit 3e34315

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
44
<TargetFrameworks>$(DefaultNetCoreTargetFramework)</TargetFrameworks>
@@ -14,6 +14,7 @@
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" />
1819
</ItemGroup>
1920

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,36 @@ 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.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+
3551
public void ConfigureServices(IServiceCollection services)
3652
{
3753
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
3854

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+
3962
services.AddAuthentication(sharedOptions =>
4063
{
41-
sharedOptions.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
42-
sharedOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
64+
sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
4365
sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
4466
})
4567
.AddCookie()
@@ -84,6 +106,7 @@ public void ConfigureServices(IServiceCollection services)
84106
public void Configure(IApplicationBuilder app, IOptionsMonitor<OpenIdConnectOptions> optionsMonitor)
85107
{
86108
app.UseDeveloperExceptionPage();
109+
app.UseCookiePolicy(); // Before UseAuthentication or anything else that writes cookies.
87110
app.UseAuthentication();
88111

89112
app.Run(async context =>

0 commit comments

Comments
 (0)