From 17dc23a024c1219ec58c48199f8d4f23117cf348 Mon Sep 17 00:00:00 2001 From: Kiran Challa Date: Thu, 26 Jan 2017 15:20:22 -0800 Subject: [PATCH] [Fixes #5637] Make CookieName of CookieTempDataProvider configurable --- .../CookieTempDataProviderOptions.cs | 5 +++ .../ViewFeatures/CookieTempDataProvider.cs | 18 ++++---- .../CookieTempDataProviderTest.cs | 41 +++++++++++++++++-- 3 files changed, 52 insertions(+), 12 deletions(-) diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/CookieTempDataProviderOptions.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/CookieTempDataProviderOptions.cs index 343924c0ca..281c10e0d7 100644 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/CookieTempDataProviderOptions.cs +++ b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/CookieTempDataProviderOptions.cs @@ -22,5 +22,10 @@ namespace Microsoft.AspNetCore.Mvc /// The domain set on a cookie. Defaults to null. /// public string Domain { get; set; } + + /// + /// The name of the cookie which stores TempData. Defaults to . + /// + public string CookieName { get; set; } = CookieTempDataProvider.CookieName; } } diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/CookieTempDataProvider.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/CookieTempDataProvider.cs index 3255d47553..c3f21802c9 100644 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/CookieTempDataProvider.cs +++ b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/CookieTempDataProvider.cs @@ -23,14 +23,14 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures private readonly IDataProtector _dataProtector; private readonly TempDataSerializer _tempDataSerializer; private readonly ChunkingCookieManager _chunkingCookieManager; - private readonly IOptions _options; + private readonly CookieTempDataProviderOptions _options; public CookieTempDataProvider(IDataProtectionProvider dataProtectionProvider, IOptions options) { _dataProtector = dataProtectionProvider.CreateProtector(Purpose); _tempDataSerializer = new TempDataSerializer(); _chunkingCookieManager = new ChunkingCookieManager(); - _options = options; + _options = options.Value; } public IDictionary LoadTempData(HttpContext context) @@ -40,9 +40,9 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures throw new ArgumentNullException(nameof(context)); } - if (context.Request.Cookies.ContainsKey(CookieName)) + if (context.Request.Cookies.ContainsKey(_options.CookieName)) { - var encodedValue = _chunkingCookieManager.GetRequestCookie(context, CookieName); + var encodedValue = _chunkingCookieManager.GetRequestCookie(context, _options.CookieName); if (!string.IsNullOrEmpty(encodedValue)) { var protectedData = Base64UrlTextEncoder.Decode(encodedValue); @@ -63,7 +63,7 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures var cookieOptions = new CookieOptions() { - Domain = string.IsNullOrEmpty(_options.Value.Domain) ? null : _options.Value.Domain, + Domain = string.IsNullOrEmpty(_options.Domain) ? null : _options.Domain, HttpOnly = true, Secure = context.Request.IsHttps, }; @@ -75,19 +75,19 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures var bytes = _tempDataSerializer.Serialize(values); bytes = _dataProtector.Protect(bytes); var encodedValue = Base64UrlTextEncoder.Encode(bytes); - _chunkingCookieManager.AppendResponseCookie(context, CookieName, encodedValue, cookieOptions); + _chunkingCookieManager.AppendResponseCookie(context, _options.CookieName, encodedValue, cookieOptions); } else { - _chunkingCookieManager.DeleteCookie(context, CookieName, cookieOptions); + _chunkingCookieManager.DeleteCookie(context, _options.CookieName, cookieOptions); } } private void SetCookiePath(HttpContext httpContext, CookieOptions cookieOptions) { - if (!string.IsNullOrEmpty(_options.Value.Path)) + if (!string.IsNullOrEmpty(_options.Path)) { - cookieOptions.Path = _options.Value.Path; + cookieOptions.Path = _options.Path; } else { diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewFeatures/CookieTempDataProviderTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewFeatures/CookieTempDataProviderTest.cs index 15a910cd79..902c17da28 100644 --- a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewFeatures/CookieTempDataProviderTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewFeatures/CookieTempDataProviderTest.cs @@ -6,9 +6,9 @@ using System.Collections; using System.Collections.Generic; using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.WebUtilities; using Microsoft.AspNetCore.Http.Internal; using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal; +using Microsoft.AspNetCore.WebUtilities; using Microsoft.Extensions.Options; using Moq; using Xunit; @@ -17,6 +17,41 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures { public class CookieTempDataProviderTest { + [Fact] + public void SaveTempData_UsesCookieName_FromOptions() + { + // Arrange + var exepectedCookieName = "TestCookieName"; + var values = new Dictionary(); + values.Add("int", 10); + + var tempDataProviderStore = new TempDataSerializer(); + var expectedDataToProtect = tempDataProviderStore.Serialize(values); + var expectedDataInCookie = Base64UrlTextEncoder.Encode(expectedDataToProtect); + var tempDataProvider = GetProvider(dataProtector: null, options: new CookieTempDataProviderOptions() + { + CookieName = exepectedCookieName + }); + + var responseCookies = new MockResponseCookieCollection(); + var httpContext = new Mock(); + httpContext + .SetupGet(hc => hc.Request.PathBase) + .Returns("/"); + httpContext + .Setup(hc => hc.Response.Cookies) + .Returns(responseCookies); + + // Act + tempDataProvider.SaveTempData(httpContext.Object, values); + + // Assert + Assert.Contains(responseCookies, (cookie) => cookie.Key == exepectedCookieName); + var cookieInfo = responseCookies[exepectedCookieName]; + Assert.Equal(expectedDataInCookie, cookieInfo.Value); + Assert.Equal("/", cookieInfo.Options.Path); + } + [Fact] public void LoadTempData_ReturnsEmptyDictionary_WhenNoCookieDataIsAvailable() { @@ -583,11 +618,11 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures private CookieTempDataProvider GetProvider(IDataProtector dataProtector = null, CookieTempDataProviderOptions options = null) { - if(dataProtector == null) + if (dataProtector == null) { dataProtector = new PassThroughDataProtector(); } - if(options == null) + if (options == null) { options = new CookieTempDataProviderOptions(); }