Skip to content

Instantly share code, notes, and snippets.

@natemcmaster
Last active March 7, 2016 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natemcmaster/e51aa33cf77416772afc to your computer and use it in GitHub Desktop.
Save natemcmaster/e51aa33cf77416772afc to your computer and use it in GitHub Desktop.
no httponly cookies :(
{
"server": "Microsoft.AspNetCore.Server.Kestrel",
"server.urls": "http://localhost:5000"
}
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<!--To inherit the global NuGet package sources remove the <clear/> line below -->
<clear />
<add key="AspNetVNext" value="https://www.myget.org/F/aspnetcidev/api/v3/index.json" />
<add key="api.nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>
</configuration>
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel.Filter;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.PlatformAbstractions;
namespace SampleApp
{
public class Startup
{
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory, IApplicationEnvironment env)
{
loggerFactory.AddConsole(LogLevel.Warning);
app.UseKestrelConnectionLogging();
app.Run(async context =>
{
context.Response.Cookies.Append("corefx-httponly1", "true", new CookieOptions
{
HttpOnly = true,
Secure = false,
Path = "/"
});
context.Response.Cookies.Append("corefx-httponly2", "false", new CookieOptions
{
HttpOnly = false,
Secure = false,
Path = "/"
});
context.Response.ContentLength = 11;
context.Response.ContentType = "text/plain";
await context.Response.WriteAsync("Hello world");
});
}
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseDefaultConfiguration(args)
.UseApplicationBasePath(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build();
var serverThread = new Thread(host.Run);
serverThread.Start();
var test = new Test();
var testThread = new Thread(test.Run);
testThread.Start();
serverThread.Join();
testThread.Join();
}
}
public class Test
{
public void Run()
{
var testUri = new Uri("http://localhost:5000");
var handler = new HttpClientHandler
{
UseCookies = true
};
var client = new HttpClient(handler);
var response = client.GetAsync(testUri).GetAwaiter().GetResult();
// request cookies
var requestCookies = response.Headers
.Where(k => k.Key.Equals("Set-Cookie", StringComparison.OrdinalIgnoreCase))
.SelectMany(k => k.Value);
Console.WriteLine("Request has {0} cookies", requestCookies.Count());
foreach (var cookieString in requestCookies)
{
Console.WriteLine("Request cookie {0}", cookieString);
}
// handler cookies
var handlerCookies = handler.CookieContainer.GetCookies(testUri);
Console.WriteLine("Handler has {0} cookies", handlerCookies.Count);
foreach (Cookie cookie in handlerCookies)
{
Console.WriteLine("Handler cookie: {0} {1} {2} {3}", cookie.Name, cookie.Value, cookie.Path, cookie.HttpOnly);
}
}
}
}
{
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": true
},
"dependencies": {
"NETStandard.Library": "1.0.0-rc2-23811",
"System.Net.Http": "4.0.1-rc2-*",
"Microsoft.AspNetCore.Internal.libuv-Darwin": {
"version": "1.0.0-*",
"type": "build"
},
"Microsoft.AspNetCore.Internal.libuv-Windows": {
"version": "1.0.0-*",
"type": "build"
},
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0-*",
"Microsoft.Extensions.Logging.Console": "1.0.0-*"
},
"frameworks": {
"netstandardapp1.5": {
"dependencies": {
"NETStandard.Library": "1.0.0-*",
"System.Console": "4.0.0-*"
},
"imports": [
"dnxcore50"
]
}
},
"content": ["hosting.json"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment