Skip to content

Commit

Permalink
Adds MaxAge property to CookieOptions/Builder (#904)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkotalik authored Aug 22, 2017
1 parent 5b58a6a commit 6657f4c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/Microsoft.AspNetCore.Http.Abstractions/CookieBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public virtual string Name
/// </summary>
public virtual TimeSpan? Expiration { get; set; }

/// <summary>
/// Gets or sets the max-age for the cookie.
/// </summary>
public virtual TimeSpan? MaxAge { get; set; }

/// <summary>
/// Creates the cookie options from the given <paramref name="context"/>.
/// </summary>
Expand All @@ -92,6 +97,7 @@ public virtual CookieOptions Build(HttpContext context, DateTimeOffset expiresFr
Path = Path ?? "/",
SameSite = SameSite,
HttpOnly = HttpOnly,
MaxAge = MaxAge,
Domain = Domain,
Secure = SecurePolicy == CookieSecurePolicy.Always || (SecurePolicy == CookieSecurePolicy.SameAsRequest && context.Request.IsHttps),
Expires = Expiration.HasValue ? expiresFrom.Add(Expiration.Value) : default(DateTimeOffset?)
Expand Down
6 changes: 6 additions & 0 deletions src/Microsoft.AspNetCore.Http.Features/CookieOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,11 @@ public CookieOptions()
/// </summary>
/// <returns>true if a cookie must not be accessible by client-side script; otherwise, false.</returns>
public bool HttpOnly { get; set; }

/// <summary>
/// Gets or sets the max-age for the cookie.
/// </summary>
/// <returns>The max-age date and time for the cookie.</returns>
public TimeSpan? MaxAge { get; set; }
}
}
1 change: 1 addition & 0 deletions src/Microsoft.AspNetCore.Http/Internal/ResponseCookies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public void Append(string key, string value, CookieOptions options)
Domain = options.Domain,
Path = options.Path,
Expires = options.Expires,
MaxAge = options.MaxAge,
Secure = options.Secure,
SameSite = (Net.Http.Headers.SameSiteMode)options.SameSite,
HttpOnly = options.HttpOnly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ public void ComputesExpiration()
Assert.Equal(now.AddHours(1), options.Expires);
}

[Fact]
public void ComputesMaxAge()
{
Assert.Null(new CookieBuilder().Build(new DefaultHttpContext()).MaxAge);

var now = TimeSpan.FromHours(1);
var options = new CookieBuilder { MaxAge = now }.Build(new DefaultHttpContext());
Assert.Equal(now, options.MaxAge);
}

[Fact]
public void CookieBuilderPreservesDefaultPath()
{
Expand Down
19 changes: 17 additions & 2 deletions test/Microsoft.AspNetCore.Http.Tests/ResponseCookiesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Text;
using Microsoft.AspNetCore.Http.Internal;
using Microsoft.Extensions.ObjectPool;
using Microsoft.Net.Http.Headers;
using Xunit;

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

[Fact]
public void ProvidesMaxAgeWithCookieOptionsArgumentExpectMaxAgeToBeSet()
{
var headers = new HeaderDictionary();
var cookies = new ResponseCookies(headers, null);
var cookieOptions = new CookieOptions();
var maxAgeTime = TimeSpan.FromHours(1);
cookieOptions.MaxAge = TimeSpan.FromHours(1);
var testcookie = "TestCookie";

cookies.Append(testcookie, testcookie, cookieOptions);

var cookieHeaderValues = headers[HeaderNames.SetCookie];
Assert.Equal(1, cookieHeaderValues.Count);
Assert.Contains($"max-age={maxAgeTime.TotalSeconds.ToString()}", cookieHeaderValues[0]);
}

public static TheoryData EscapesKeyValuesBeforeSettingCookieData
{
get
Expand Down

0 comments on commit 6657f4c

Please sign in to comment.