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

Commit 788619b

Browse files
author
Nate McMaster
committed
Add CookieBuilder to CookieTempDataProviderOptions and obsolete duplicate API
1 parent d917504 commit 788619b

File tree

3 files changed

+66
-20
lines changed

3 files changed

+66
-20
lines changed

src/Microsoft.AspNetCore.Mvc.ViewFeatures/CookieTempDataProviderOptions.cs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using Microsoft.AspNetCore.Http;
56
using Microsoft.AspNetCore.Mvc.ViewFeatures;
67

@@ -11,21 +12,65 @@ namespace Microsoft.AspNetCore.Mvc
1112
/// </summary>
1213
public class CookieTempDataProviderOptions
1314
{
15+
private CookieBuilder _cookieBuilder = new CookieBuilder
16+
{
17+
Name = CookieTempDataProvider.CookieName,
18+
HttpOnly = true,
19+
SameSite = SameSiteMode.Strict,
20+
SecurePolicy = CookieSecurePolicy.SameAsRequest,
21+
};
22+
23+
/// <summary>
24+
/// <para>
25+
/// Determines the settings used to create the cookie in <see cref="CookieTempDataProvider"/>.
26+
/// </para>
27+
/// <para>
28+
/// <see cref="CookieBuilder.SameSite"/> defaults to <see cref="SameSiteMode.Strict"/>.
29+
/// <see cref="CookieBuilder.SecurePolicy"/> defaults to <see cref="CookieSecurePolicy.SameAsRequest" />.
30+
/// <see cref="CookieBuilder.HttpOnly"/> defaults to <c>true</c>
31+
/// </para>
32+
/// </summary>
33+
public CookieBuilder Cookie
34+
{
35+
get => _cookieBuilder;
36+
set => _cookieBuilder = value ?? throw new ArgumentNullException(nameof(value));
37+
}
38+
39+
#region Obsolete API
1440
/// <summary>
41+
/// <para>
42+
/// This property is obsolete and will be removed in a future version. The recommended alternative is <seealso cref="CookieBuilder.Path"/> on <see cref="Cookie"/>.
43+
/// </para>
44+
/// <para>
1545
/// The path set on the cookie. If set to <c>null</c>, the "path" attribute on the cookie is set to the current
1646
/// request's <see cref="HttpRequest.PathBase"/> value. If the value of <see cref="HttpRequest.PathBase"/> is
1747
/// <c>null</c> or empty, then the "path" attribute is set to the value of <see cref="CookieOptions.Path"/>.
48+
/// </para>
1849
/// </summary>
19-
public string Path { get; set; }
50+
[Obsolete("This property is obsolete and will be removed in a future version. The recommended alternative is " + nameof(Cookie) + "." + nameof(CookieBuilder.Path) + ".")]
51+
public string Path { get => Cookie.Path; set => Cookie.Path = value; }
2052

2153
/// <summary>
54+
/// <para>
55+
/// This property is obsolete and will be removed in a future version. The recommended alternative is <seealso cref="CookieBuilder.Domain"/> on <see cref="Cookie"/>.
56+
/// </para>
57+
/// <para>
2258
/// The domain set on a cookie. Defaults to <c>null</c>.
59+
/// </para>
2360
/// </summary>
24-
public string Domain { get; set; }
61+
[Obsolete("This property is obsolete and will be removed in a future version. The recommended alternative is " + nameof(Cookie) + "." + nameof(CookieBuilder.Domain) + ".")]
62+
public string Domain { get => Cookie.Domain; set => Cookie.Domain = value; }
2563

2664
/// <summary>
65+
/// <para>
66+
/// This property is obsolete and will be removed in a future version. The recommended alternative is <seealso cref="CookieBuilder.Name"/> on <see cref="Cookie"/>.
67+
/// </para>
68+
/// <para>
2769
/// The name of the cookie which stores TempData. Defaults to <see cref="CookieTempDataProvider.CookieName"/>.
70+
/// </para>
2871
/// </summary>
72+
[Obsolete("This property is obsolete and will be removed in a future version. The recommended alternative is " + nameof(Cookie) + "." + nameof(CookieBuilder.Name) + ".")]
2973
public string CookieName { get; set; } = CookieTempDataProvider.CookieName;
74+
#endregion
3075
}
3176
}

src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/CookieTempDataProvider.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public IDictionary<string, object> LoadTempData(HttpContext context)
3939
throw new ArgumentNullException(nameof(context));
4040
}
4141

42-
if (context.Request.Cookies.ContainsKey(_options.CookieName))
42+
if (context.Request.Cookies.ContainsKey(_options.Cookie.Name))
4343
{
44-
var encodedValue = _chunkingCookieManager.GetRequestCookie(context, _options.CookieName);
44+
var encodedValue = _chunkingCookieManager.GetRequestCookie(context, _options.Cookie.Name);
4545
if (!string.IsNullOrEmpty(encodedValue))
4646
{
4747
var protectedData = Base64UrlTextEncoder.Decode(encodedValue);
@@ -60,13 +60,7 @@ public void SaveTempData(HttpContext context, IDictionary<string, object> values
6060
throw new ArgumentNullException(nameof(context));
6161
}
6262

63-
var cookieOptions = new CookieOptions()
64-
{
65-
Domain = string.IsNullOrEmpty(_options.Domain) ? null : _options.Domain,
66-
HttpOnly = true,
67-
SameSite = SameSiteMode.Strict,
68-
Secure = context.Request.IsHttps,
69-
};
63+
var cookieOptions = _options.Cookie.Build(context);
7064
SetCookiePath(context, cookieOptions);
7165

7266
var hasValues = (values != null && values.Count > 0);
@@ -75,19 +69,19 @@ public void SaveTempData(HttpContext context, IDictionary<string, object> values
7569
var bytes = _tempDataSerializer.Serialize(values);
7670
bytes = _dataProtector.Protect(bytes);
7771
var encodedValue = Base64UrlTextEncoder.Encode(bytes);
78-
_chunkingCookieManager.AppendResponseCookie(context, _options.CookieName, encodedValue, cookieOptions);
72+
_chunkingCookieManager.AppendResponseCookie(context, _options.Cookie.Name, encodedValue, cookieOptions);
7973
}
8074
else
8175
{
82-
_chunkingCookieManager.DeleteCookie(context, _options.CookieName, cookieOptions);
76+
_chunkingCookieManager.DeleteCookie(context, _options.Cookie.Name, cookieOptions);
8377
}
8478
}
8579

8680
private void SetCookiePath(HttpContext httpContext, CookieOptions cookieOptions)
8781
{
88-
if (!string.IsNullOrEmpty(_options.Path))
82+
if (!string.IsNullOrEmpty(_options.Cookie.Path))
8983
{
90-
cookieOptions.Path = _options.Path;
84+
cookieOptions.Path = _options.Cookie.Path;
9185
}
9286
else
9387
{

test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewFeatures/CookieTempDataProviderTest.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class CookieTempDataProviderTest
2121
public void SaveTempData_UsesCookieName_FromOptions()
2222
{
2323
// Arrange
24-
var exepectedCookieName = "TestCookieName";
24+
var expectedCookieName = "TestCookieName";
2525
var values = new Dictionary<string, object>();
2626
values.Add("int", 10);
2727

@@ -30,7 +30,7 @@ public void SaveTempData_UsesCookieName_FromOptions()
3030
var expectedDataInCookie = Base64UrlTextEncoder.Encode(expectedDataToProtect);
3131
var tempDataProvider = GetProvider(dataProtector: null, options: new CookieTempDataProviderOptions()
3232
{
33-
CookieName = exepectedCookieName
33+
Cookie = { Name = expectedCookieName }
3434
});
3535

3636
var responseCookies = new MockResponseCookieCollection();
@@ -46,8 +46,8 @@ public void SaveTempData_UsesCookieName_FromOptions()
4646
tempDataProvider.SaveTempData(httpContext.Object, values);
4747

4848
// Assert
49-
Assert.Contains(responseCookies, (cookie) => cookie.Key == exepectedCookieName);
50-
var cookieInfo = responseCookies[exepectedCookieName];
49+
Assert.Contains(responseCookies, (cookie) => cookie.Key == expectedCookieName);
50+
var cookieInfo = responseCookies[expectedCookieName];
5151
Assert.Equal(expectedDataInCookie, cookieInfo.Value);
5252
Assert.Equal("/", cookieInfo.Options.Path);
5353
}
@@ -230,7 +230,14 @@ public void SaveTempData_CustomProviderOptions_SetsCookie_WithAppropriateCookieO
230230
var dataProtector = new PassThroughDataProtector();
231231
var tempDataProvider = GetProvider(
232232
dataProtector,
233-
new CookieTempDataProviderOptions() { Path = optionsPath, Domain = optionsDomain });
233+
new CookieTempDataProviderOptions
234+
{
235+
Cookie =
236+
{
237+
Path = optionsPath,
238+
Domain = optionsDomain
239+
}
240+
});
234241
var responseCookies = new MockResponseCookieCollection();
235242
var httpContext = new Mock<HttpContext>();
236243
httpContext

0 commit comments

Comments
 (0)