Skip to content
This repository was archived by the owner on Nov 22, 2018. It is now read-only.

Commit 83bc308

Browse files
author
Justin Kotalik
committed
#106 Fix: Adds Secure Cookie flag and tests
1 parent db5fd0e commit 83bc308

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

src/Microsoft.AspNetCore.Session/SessionMiddleware.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ private void SetCookie()
156156
HttpOnly = _options.CookieHttpOnly,
157157
Path = _options.CookiePath ?? SessionDefaults.CookiePath,
158158
};
159+
if (_options.CookieSecure == CookieSecurePolicy.SameAsRequest)
160+
{
161+
cookieOptions.Secure = _context.Request.IsHttps;
162+
}
163+
else
164+
{
165+
cookieOptions.Secure = _options.CookieSecure == CookieSecurePolicy.Always;
166+
}
159167

160168
_context.Response.Cookies.Append(_options.CookieName, _cookieValue, cookieOptions);
161169

src/Microsoft.AspNetCore.Session/SessionOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using Microsoft.AspNetCore.Http;
56
using Microsoft.AspNetCore.Session;
67

78
namespace Microsoft.AspNetCore.Builder
@@ -35,6 +36,10 @@ public class SessionOptions
3536
/// </summary>
3637
public bool CookieHttpOnly { get; set; } = true;
3738

39+
/// <summary>
40+
/// Determines if the cookie should only be transmitted on HTTPS requests.
41+
public CookieSecurePolicy CookieSecure { get; set; } = CookieSecurePolicy.None;
42+
3843
/// <summary>
3944
/// The IdleTimeout indicates how long the session can be idle before its contents are abandoned. Each session access
4045
/// resets the timeout. Note this only applies to the content of the session, not the cookie.

test/Microsoft.AspNetCore.Session.Tests/SessionTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,59 @@ public async Task SettingAValueCausesTheCookieToBeCreated()
8787
}
8888
}
8989

90+
[Theory]
91+
[InlineData(CookieSecurePolicy.Always, "http://example.com/testpath", true)]
92+
[InlineData(CookieSecurePolicy.Always, "https://example.com/testpath", true)]
93+
[InlineData(CookieSecurePolicy.None, "http://example.com/testpath", false)]
94+
[InlineData(CookieSecurePolicy.None, "https://example.com/testpath", false)]
95+
[InlineData(CookieSecurePolicy.SameAsRequest, "http://example.com/testpath", false)]
96+
[InlineData(CookieSecurePolicy.SameAsRequest, "https://example.com/testpath", true)]
97+
public async Task SecureSessionBasedOnHttpsAndSecurePolicy(
98+
CookieSecurePolicy cookieSecurePolicy,
99+
string requestUri,
100+
bool shouldBeSecureOnly)
101+
{
102+
var builder = new WebHostBuilder()
103+
.Configure(app =>
104+
{
105+
app.UseSession(new SessionOptions
106+
{
107+
CookieName = "TestCookie",
108+
CookieSecure = cookieSecurePolicy
109+
});
110+
app.Run(context =>
111+
{
112+
Assert.Null(context.Session.GetString("Key"));
113+
context.Session.SetString("Key", "Value");
114+
Assert.Equal("Value", context.Session.GetString("Key"));
115+
return Task.FromResult(0);
116+
});
117+
})
118+
.ConfigureServices(services =>
119+
{
120+
services.AddDistributedMemoryCache();
121+
services.AddSession();
122+
});
123+
124+
using (var server = new TestServer(builder))
125+
{
126+
var client = server.CreateClient();
127+
var response = await client.GetAsync(requestUri);
128+
response.EnsureSuccessStatusCode();
129+
IEnumerable<string> values;
130+
Assert.True(response.Headers.TryGetValues("Set-Cookie", out values));
131+
Assert.Equal(1, values.Count());
132+
if (shouldBeSecureOnly)
133+
{
134+
Assert.Contains("; secure", values.First());
135+
}
136+
else
137+
{
138+
Assert.DoesNotContain("; secure", values.First());
139+
}
140+
}
141+
}
142+
90143
[Fact]
91144
public async Task SessionCanBeAccessedOnTheNextRequest()
92145
{

0 commit comments

Comments
 (0)