This repository has been archived by the owner on Dec 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 599
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,021 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net461;netcoreapp2.1</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.CookiePolicy\Microsoft.AspNetCore.CookiePolicy.csproj" /> | ||
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Authentication.Cookies\Microsoft.AspNetCore.Authentication.Cookies.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="$(MicrosoftAspNetCoreServerIISIntegrationPackageVersion)" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="$(MicrosoftAspNetCoreServerKestrelPackageVersion)" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="$(MicrosoftExtensionsLoggingConsolePackageVersion)" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System.IO; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace CookiePolicySample | ||
{ | ||
public static class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
var host = new WebHostBuilder() | ||
.ConfigureLogging(factory => | ||
{ | ||
factory.AddConsole(); | ||
factory.AddFilter("Console", level => level >= LogLevel.Information); | ||
}) | ||
.UseKestrel() | ||
.UseContentRoot(Directory.GetCurrentDirectory()) | ||
.UseIISIntegration() | ||
.UseStartup<Startup>() | ||
.Build(); | ||
|
||
host.Run(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:1788/", | ||
"sslPort": 0 | ||
} | ||
}, | ||
"profiles": { | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"CookieSample": { | ||
"commandName": "Project", | ||
"launchBrowser": true, | ||
"applicationUrl": "http://localhost:12345", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Authentication.Cookies; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Net.Http.Headers; | ||
|
||
namespace CookiePolicySample | ||
{ | ||
public class Startup | ||
{ | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) | ||
.AddCookie(); | ||
services.Configure<CookiePolicyOptions>(options => | ||
{ | ||
options.CheckConsentNeeded = context => context.Request.PathBase.Equals("/NeedsConsent"); | ||
options.OnAppendCookie = context => { }; | ||
}); | ||
} | ||
|
||
public void Configure(IApplicationBuilder app) | ||
{ | ||
app.UseCookiePolicy(); | ||
app.UseAuthentication(); | ||
|
||
app.Map("/NeedsConsent", NestedApp); | ||
app.Map("/NeedsNoConsent", NestedApp); | ||
NestedApp(app); | ||
} | ||
|
||
private void NestedApp(IApplicationBuilder app) | ||
{ | ||
app.Run(async context => | ||
{ | ||
var path = context.Request.Path; | ||
switch (path) | ||
{ | ||
case "/Login": | ||
var user = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, "bob") }, | ||
CookieAuthenticationDefaults.AuthenticationScheme)); | ||
await context.SignInAsync(user); | ||
break; | ||
case "/Logout": | ||
await context.SignOutAsync(); | ||
break; | ||
case "/CreateTempCookie": | ||
context.Response.Cookies.Append("Temp", "1"); | ||
break; | ||
case "/RemoveTempCookie": | ||
context.Response.Cookies.Delete("Temp"); | ||
break; | ||
case "/GrantConsent": | ||
context.Features.Get<ITrackingConsentFeature>().GrantConsent(); | ||
break; | ||
case "/WithdrawConsent": | ||
context.Features.Get<ITrackingConsentFeature>().WithdrawConsent(); | ||
break; | ||
} | ||
// TODO: Debug log when cookie is suppressed | ||
await HomePage(context); | ||
}); | ||
} | ||
|
||
private async Task HomePage(HttpContext context) | ||
{ | ||
var response = context.Response; | ||
var cookies = context.Request.Cookies; | ||
response.ContentType = "text/html"; | ||
await response.WriteAsync("<html><body>\r\n"); | ||
|
||
await response.WriteAsync($"<a href=\"{context.Request.PathBase}/\">Home</a><br>\r\n"); | ||
await response.WriteAsync($"<a href=\"{context.Request.PathBase}/Login\">Login</a><br>\r\n"); | ||
await response.WriteAsync($"<a href=\"{context.Request.PathBase}/Logout\">Logout</a><br>\r\n"); | ||
await response.WriteAsync($"<a href=\"{context.Request.PathBase}/CreateTempCookie\">Create Temp Cookie</a><br>\r\n"); | ||
await response.WriteAsync($"<a href=\"{context.Request.PathBase}/RemoveTempCookie\">Remove Temp Cookie</a><br>\r\n"); | ||
await response.WriteAsync($"<a href=\"{context.Request.PathBase}/GrantConsent\">Grant Consent</a><br>\r\n"); | ||
await response.WriteAsync($"<a href=\"{context.Request.PathBase}/WithdrawConsent\">Withdraw Consent</a><br>\r\n"); | ||
await response.WriteAsync("<br>\r\n"); | ||
await response.WriteAsync($"<a href=\"/NeedsConsent{context.Request.Path}\">Needs Consent</a><br>\r\n"); | ||
await response.WriteAsync($"<a href=\"/NeedsNoConsent{context.Request.Path}\">Needs No Consent</a><br>\r\n"); | ||
await response.WriteAsync("<br>\r\n"); | ||
|
||
var feature = context.Features.Get<ITrackingConsentFeature>(); | ||
await response.WriteAsync($"Consent: <br>\r\n"); | ||
await response.WriteAsync($" - IsNeeded: {feature.IsConsentNeeded} <br>\r\n"); | ||
await response.WriteAsync($" - Has: {feature.HasConsent} <br>\r\n"); | ||
await response.WriteAsync($" - Can Track: {feature.CanTrack} <br>\r\n"); | ||
await response.WriteAsync("<br>\r\n"); | ||
|
||
await response.WriteAsync($"{cookies.Count} Request Cookies:<br>\r\n"); | ||
foreach (var cookie in cookies) | ||
{ | ||
await response.WriteAsync($" - {cookie.Key} = {cookie.Value} <br>\r\n"); | ||
} | ||
await response.WriteAsync("<br>\r\n"); | ||
|
||
var responseCookies = response.Headers[HeaderNames.SetCookie]; | ||
await response.WriteAsync($"{responseCookies.Count} Response Cookies:<br>\r\n"); | ||
foreach (var cookie in responseCookies) | ||
{ | ||
await response.WriteAsync($" - {cookie} <br>\r\n"); | ||
} | ||
|
||
await response.WriteAsync("</body></html>"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.