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

Commit 6657f4c

Browse files
authored
Adds MaxAge property to CookieOptions/Builder (#904)
1 parent 5b58a6a commit 6657f4c

File tree

5 files changed

+40
-2
lines changed

5 files changed

+40
-2
lines changed

src/Microsoft.AspNetCore.Http.Abstractions/CookieBuilder.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public virtual string Name
6767
/// </summary>
6868
public virtual TimeSpan? Expiration { get; set; }
6969

70+
/// <summary>
71+
/// Gets or sets the max-age for the cookie.
72+
/// </summary>
73+
public virtual TimeSpan? MaxAge { get; set; }
74+
7075
/// <summary>
7176
/// Creates the cookie options from the given <paramref name="context"/>.
7277
/// </summary>
@@ -92,6 +97,7 @@ public virtual CookieOptions Build(HttpContext context, DateTimeOffset expiresFr
9297
Path = Path ?? "/",
9398
SameSite = SameSite,
9499
HttpOnly = HttpOnly,
100+
MaxAge = MaxAge,
95101
Domain = Domain,
96102
Secure = SecurePolicy == CookieSecurePolicy.Always || (SecurePolicy == CookieSecurePolicy.SameAsRequest && context.Request.IsHttps),
97103
Expires = Expiration.HasValue ? expiresFrom.Add(Expiration.Value) : default(DateTimeOffset?)

src/Microsoft.AspNetCore.Http.Features/CookieOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,11 @@ public CookieOptions()
5353
/// </summary>
5454
/// <returns>true if a cookie must not be accessible by client-side script; otherwise, false.</returns>
5555
public bool HttpOnly { get; set; }
56+
57+
/// <summary>
58+
/// Gets or sets the max-age for the cookie.
59+
/// </summary>
60+
/// <returns>The max-age date and time for the cookie.</returns>
61+
public TimeSpan? MaxAge { get; set; }
5662
}
5763
}

src/Microsoft.AspNetCore.Http/Internal/ResponseCookies.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public void Append(string key, string value, CookieOptions options)
6161
Domain = options.Domain,
6262
Path = options.Path,
6363
Expires = options.Expires,
64+
MaxAge = options.MaxAge,
6465
Secure = options.Secure,
6566
SameSite = (Net.Http.Headers.SameSiteMode)options.SameSite,
6667
HttpOnly = options.HttpOnly

test/Microsoft.AspNetCore.Http.Abstractions.Tests/CookieBuilderTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ public void ComputesExpiration()
3838
Assert.Equal(now.AddHours(1), options.Expires);
3939
}
4040

41+
[Fact]
42+
public void ComputesMaxAge()
43+
{
44+
Assert.Null(new CookieBuilder().Build(new DefaultHttpContext()).MaxAge);
45+
46+
var now = TimeSpan.FromHours(1);
47+
var options = new CookieBuilder { MaxAge = now }.Build(new DefaultHttpContext());
48+
Assert.Equal(now, options.MaxAge);
49+
}
50+
4151
[Fact]
4252
public void CookieBuilderPreservesDefaultPath()
4353
{

test/Microsoft.AspNetCore.Http.Tests/ResponseCookiesTest.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +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 System.Text;
65
using Microsoft.AspNetCore.Http.Internal;
7-
using Microsoft.Extensions.ObjectPool;
86
using Microsoft.Net.Http.Headers;
97
using Xunit;
108

@@ -74,6 +72,23 @@ public void NoParamsDeleteRemovesCookieCreatedByAdd()
7472
Assert.Contains("expires=Thu, 01 Jan 1970 00:00:00 GMT", cookieHeaderValues[0]);
7573
}
7674

75+
[Fact]
76+
public void ProvidesMaxAgeWithCookieOptionsArgumentExpectMaxAgeToBeSet()
77+
{
78+
var headers = new HeaderDictionary();
79+
var cookies = new ResponseCookies(headers, null);
80+
var cookieOptions = new CookieOptions();
81+
var maxAgeTime = TimeSpan.FromHours(1);
82+
cookieOptions.MaxAge = TimeSpan.FromHours(1);
83+
var testcookie = "TestCookie";
84+
85+
cookies.Append(testcookie, testcookie, cookieOptions);
86+
87+
var cookieHeaderValues = headers[HeaderNames.SetCookie];
88+
Assert.Equal(1, cookieHeaderValues.Count);
89+
Assert.Contains($"max-age={maxAgeTime.TotalSeconds.ToString()}", cookieHeaderValues[0]);
90+
}
91+
7792
public static TheoryData EscapesKeyValuesBeforeSettingCookieData
7893
{
7994
get

0 commit comments

Comments
 (0)