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

Commit dece939

Browse files
author
Nate McMaster
committed
Add CookieBuilder property to SessionOptions and obsolete duplicated properties
1 parent 2b68dc9 commit dece939

File tree

4 files changed

+108
-34
lines changed

4 files changed

+108
-34
lines changed

Session.sln

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Microsoft Visual Studio Solution File, Format Version 12.00
22
# Visual Studio 15
3-
VisualStudioVersion = 15.0.26228.9
3+
VisualStudioVersion = 15.0.26621.2
44
MinimumVisualStudioVersion = 10.0.40219.1
55
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{E9D63F97-6078-42AD-BFD3-F956BF921BB5}"
66
EndProject
@@ -15,6 +15,16 @@ EndProject
1515
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SessionSample", "samples\SessionSample\SessionSample.csproj", "{FE0B9969-3BDE-4A7D-BE1B-47EAE8DBF365}"
1616
EndProject
1717
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionItems", "SolutionItems", "{3B45F658-5BF1-4E07-BE9C-6F5110AC2277}"
18+
ProjectSection(SolutionItems) = preProject
19+
.gitattributes = .gitattributes
20+
.gitignore = .gitignore
21+
.travis.yml = .travis.yml
22+
appveyor.yml = appveyor.yml
23+
NuGet.config = NuGet.config
24+
NuGetPackageVerifier.json = NuGetPackageVerifier.json
25+
README.md = README.md
26+
version.props = version.props
27+
EndProjectSection
1828
EndProject
1929
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{4F21221F-2813-41B7-AAFC-E03FD52971CC}"
2030
ProjectSection(SolutionItems) = preProject
@@ -50,4 +60,7 @@ Global
5060
{FE0B9969-3BDE-4A7D-BE1B-47EAE8DBF365} = {94E80ED2-9F27-40AC-A9EF-C707BDFAA3BE}
5161
{4F21221F-2813-41B7-AAFC-E03FD52971CC} = {3B45F658-5BF1-4E07-BE9C-6F5110AC2277}
5262
EndGlobalSection
63+
GlobalSection(ExtensibilityGlobals) = postSolution
64+
SolutionGuid = {6AE224B9-B604-4E47-9617-9D114DAE9BE5}
65+
EndGlobalSection
5366
EndGlobal

src/Microsoft.AspNetCore.Session/SessionMiddleware.cs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public async Task Invoke(HttpContext context)
8383
{
8484
var isNewSessionKey = false;
8585
Func<bool> tryEstablishSession = ReturnTrue;
86-
var cookieValue = context.Request.Cookies[_options.CookieName];
86+
var cookieValue = context.Request.Cookies[_options.Cookie.Name];
8787
var sessionKey = CookieProtection.Unprotect(_dataProtector, cookieValue, _logger);
8888
if (string.IsNullOrWhiteSpace(sessionKey) || sessionKey.Length != SessionKeyLength)
8989
{
@@ -150,23 +150,9 @@ private static Task OnStartingCallback(object state)
150150

151151
private void SetCookie()
152152
{
153-
var cookieOptions = new CookieOptions
154-
{
155-
Domain = _options.CookieDomain,
156-
SameSite = _options.SameSiteMode,
157-
HttpOnly = _options.CookieHttpOnly,
158-
Path = _options.CookiePath ?? SessionDefaults.CookiePath,
159-
};
160-
if (_options.CookieSecure == CookieSecurePolicy.SameAsRequest)
161-
{
162-
cookieOptions.Secure = _context.Request.IsHttps;
163-
}
164-
else
165-
{
166-
cookieOptions.Secure = _options.CookieSecure == CookieSecurePolicy.Always;
167-
}
153+
var cookieOptions = _options.Cookie.Build(_context);
168154

169-
_context.Response.Cookies.Append(_options.CookieName, _cookieValue, cookieOptions);
155+
_context.Response.Cookies.Append(_options.Cookie.Name, _cookieValue, cookieOptions);
170156

171157
_context.Response.Headers["Cache-Control"] = "no-cache";
172158
_context.Response.Headers["Pragma"] = "no-cache";

src/Microsoft.AspNetCore.Session/SessionOptions.cs

Lines changed: 86 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,117 @@ namespace Microsoft.AspNetCore.Builder
1212
/// </summary>
1313
public class SessionOptions
1414
{
15+
private CookieBuilder _cookieBuilder = new SessionCookieBuilder();
16+
17+
/// <summary>
18+
/// Determines the settings used to create the cookie.
19+
/// <para>
20+
/// <see cref="CookieBuilder.Name"/> defaults to <see cref="SessionDefaults.CookieName"/>.
21+
/// <see cref="CookieBuilder.Path"/> defaults to <see cref="SessionDefaults.CookiePath"/>.
22+
/// <see cref="CookieBuilder.SameSite"/> defaults to <see cref="SameSiteMode.Lax"/>.
23+
/// <see cref="CookieBuilder.HttpOnly"/> defaults to <c>true</c>
24+
/// </para>
25+
/// </summary>
26+
public CookieBuilder Cookie
27+
{
28+
get => _cookieBuilder;
29+
set => _cookieBuilder = value ?? throw new ArgumentNullException(nameof(value));
30+
}
31+
32+
/// <summary>
33+
/// The IdleTimeout indicates how long the session can be idle before its contents are abandoned. Each session access
34+
/// resets the timeout. Note this only applies to the content of the session, not the cookie.
35+
/// </summary>
36+
public TimeSpan IdleTimeout { get; set; } = TimeSpan.FromMinutes(20);
37+
38+
#region Obsolete API
1539
/// <summary>
40+
/// <para>
41+
/// This property is obsolete and will be removed in a future version. The recommended alternative is <seealso cref="CookieBuilder.Name"/> on <see cref="Cookie"/>.
42+
/// </para>
43+
/// <para>
1644
/// Determines the cookie name used to persist the session ID.
17-
/// Defaults to <see cref="SessionDefaults.CookieName"/>.
45+
/// </para>
1846
/// </summary>
19-
public string CookieName { get; set; } = SessionDefaults.CookieName;
47+
[Obsolete("This property is obsolete and will be removed in a future version. The recommended alternative is " + nameof(Cookie) + "." + nameof(CookieBuilder.Name) + ".")]
48+
public string CookieName { get => Cookie.Name; set => Cookie.Name = value; }
2049

2150
/// <summary>
51+
/// <para>
52+
/// This property is obsolete and will be removed in a future version. The recommended alternative is <seealso cref="CookieBuilder.Domain"/> on <see cref="Cookie"/>.
53+
/// </para>
54+
/// <para>
2255
/// Determines the domain used to create the cookie. Is not provided by default.
56+
/// </para>
2357
/// </summary>
24-
public string CookieDomain { get; set; }
58+
[Obsolete("This property is obsolete and will be removed in a future version. The recommended alternative is " + nameof(Cookie) + "." + nameof(CookieBuilder.Domain) + ".")]
59+
public string CookieDomain { get => Cookie.Domain; set => Cookie.Domain = value; }
2560

2661
/// <summary>
62+
/// <para>
63+
/// This property is obsolete and will be removed in a future version. The recommended alternative is <seealso cref="CookieBuilder.Path"/> on <see cref="Cookie"/>.
64+
/// </para>
65+
/// <para>
2766
/// Determines the path used to create the cookie.
2867
/// Defaults to <see cref="SessionDefaults.CookiePath"/>.
68+
/// </para>
2969
/// </summary>
30-
public string CookiePath { get; set; } = SessionDefaults.CookiePath;
70+
[Obsolete("This property is obsolete and will be removed in a future version. The recommended alternative is " + nameof(Cookie) + "." + nameof(CookieBuilder.Path) + ".")]
71+
public string CookiePath { get => Cookie.Path; set => Cookie.Path = value; }
3172

3273
/// <summary>
74+
/// <para>
75+
/// This property is obsolete and will be removed in a future version. The recommended alternative is <seealso cref="CookieBuilder.HttpOnly"/> on <see cref="Cookie"/>.
76+
/// </para>
77+
/// <para>
3378
/// Determines if the browser should allow the cookie to be accessed by client-side JavaScript. The
3479
/// default is true, which means the cookie will only be passed to HTTP requests and is not made available
3580
/// to script on the page.
81+
/// </para>
3682
/// </summary>
37-
public bool CookieHttpOnly { get; set; } = true;
83+
[Obsolete("This property is obsolete and will be removed in a future version. The recommended alternative is " + nameof(Cookie) + "." + nameof(CookieBuilder.HttpOnly) + ".")]
84+
public bool CookieHttpOnly { get => Cookie.HttpOnly; set => Cookie.HttpOnly = value; }
3885

3986
/// <summary>
87+
/// <para>
88+
/// This property is obsolete and will be removed in a future version. The recommended alternative is <seealso cref="CookieBuilder.SameSite"/> on <see cref="Cookie"/>.
89+
/// </para>
90+
/// <para>
4091
/// Determines if the browser should allow the cookie to be attached to same-site or cross-site requests. The
4192
/// default is Lax, which means the cookie is allowed to be attached to same-site and safe cross-site requests.
93+
/// </para>
4294
/// </summary>
43-
public SameSiteMode SameSiteMode { get; set; } = SameSiteMode.Lax;
95+
[Obsolete("This property is obsolete and will be removed in a future version. The recommended alternative is " + nameof(Cookie) + "." + nameof(CookieBuilder.SameSite) + ".")]
96+
public SameSiteMode SameSiteMode { get => Cookie.SameSite; set => Cookie.SameSite = value; }
4497

4598
/// <summary>
46-
/// Determines if the cookie should only be transmitted on HTTPS requests.
99+
/// <para>
100+
/// This property is obsolete and will be removed in a future version. The recommended alternative is <seealso cref="CookieBuilder.SecurePolicy"/> on <see cref="Cookie"/>.
101+
/// </para>
102+
/// <para>
103+
/// Determines if the cookie should only be transmitted on HTTPS requests.
104+
/// </para>
47105
/// </summary>
48-
public CookieSecurePolicy CookieSecure { get; set; } = CookieSecurePolicy.None;
106+
[Obsolete("This property is obsolete and will be removed in a future version. The recommended alternative is " + nameof(Cookie) + "." + nameof(CookieBuilder.SecurePolicy) + ".")]
107+
public CookieSecurePolicy CookieSecure { get => Cookie.SecurePolicy; set => Cookie.SecurePolicy = value; }
108+
#endregion
49109

50-
/// <summary>
51-
/// The IdleTimeout indicates how long the session can be idle before its contents are abandoned. Each session access
52-
/// resets the timeout. Note this only applies to the content of the session, not the cookie.
53-
/// </summary>
54-
public TimeSpan IdleTimeout { get; set; } = TimeSpan.FromMinutes(20);
110+
private class SessionCookieBuilder : CookieBuilder
111+
{
112+
public SessionCookieBuilder()
113+
{
114+
Name = SessionDefaults.CookieName;
115+
Path = SessionDefaults.CookiePath;
116+
SecurePolicy = CookieSecurePolicy.None;
117+
SameSite = SameSiteMode.Lax;
118+
HttpOnly = true;
119+
}
120+
121+
public override TimeSpan? Expiration
122+
{
123+
get => null;
124+
set => throw new InvalidOperationException(nameof(Expiration) + " cannot be set for the cookie defined by " + nameof(SessionOptions));
125+
}
126+
}
55127
}
56-
}
128+
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,11 @@ public async Task SecureSessionBasedOnHttpsAndSecurePolicy(
105105
{
106106
app.UseSession(new SessionOptions
107107
{
108-
CookieName = "TestCookie",
109-
CookieSecure = cookieSecurePolicy
108+
Cookie =
109+
{
110+
Name = "TestCookie",
111+
SecurePolicy = cookieSecurePolicy
112+
}
110113
});
111114
app.Run(context =>
112115
{

0 commit comments

Comments
 (0)