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

Commit 9d39c8a

Browse files
author
Nate McMaster
committed
Add CookieBuilder to CookieTempDataProviderOptions and obsolete duplicate API
1 parent 293ac81 commit 9d39c8a

File tree

3 files changed

+61
-17
lines changed

3 files changed

+61
-17
lines changed

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

Lines changed: 45 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,63 @@ 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+
1439
/// <summary>
40+
/// <para>
41+
/// This property is obsolete and will be removed in a future version. The recommended alternative is <seealso cref="CookieBuilder.Path"/> on <see cref="Cookie"/>.
42+
/// </para>
43+
/// <para>
1544
/// The path set on the cookie. If set to <c>null</c>, the "path" attribute on the cookie is set to the current
1645
/// request's <see cref="HttpRequest.PathBase"/> value. If the value of <see cref="HttpRequest.PathBase"/> is
1746
/// <c>null</c> or empty, then the "path" attribute is set to the value of <see cref="CookieOptions.Path"/>.
47+
/// </para>
1848
/// </summary>
19-
public string Path { get; set; }
49+
[Obsolete("This property is obsolete and will be removed in a future version. The recommended alternative is " + nameof(Cookie) + "." + nameof(CookieBuilder.Path) + ".")]
50+
public string Path { get => Cookie.Path; set => Cookie.Path = value; }
2051

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

2663
/// <summary>
64+
/// <para>
65+
/// This property is obsolete and will be removed in a future version. The recommended alternative is <seealso cref="CookieBuilder.Name"/> on <see cref="Cookie"/>.
66+
/// </para>
67+
/// <para>
2768
/// The name of the cookie which stores TempData. Defaults to <see cref="CookieTempDataProvider.CookieName"/>.
69+
/// </para>
2870
/// </summary>
71+
[Obsolete("This property is obsolete and will be removed in a future version. The recommended alternative is " + nameof(Cookie) + "." + nameof(CookieBuilder.Name) + ".")]
2972
public string CookieName { get; set; } = CookieTempDataProvider.CookieName;
3073
}
3174
}

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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = exepectedCookieName }
3434
});
3535

3636
var responseCookies = new MockResponseCookieCollection();
@@ -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)