public void SaveTempData_ProtectsAnd_Base64UrlEncodesDataAnd_SetsCookie() { // Arrange var values = new Dictionary <string, object>(); values.Add("int", 10); var tempDataProviderStore = new TempDataSerializer(); var expectedDataToProtect = tempDataProviderStore.Serialize(values); var expectedDataInCookie = WebEncoders.Base64UrlEncode(expectedDataToProtect); var dataProtector = new PassThroughDataProtector(); var tempDataProvider = GetProvider(dataProtector); var responseCookies = new MockResponseCookieCollection(); var httpContext = new Mock <HttpContext>(); httpContext .SetupGet(hc => hc.Request.PathBase) .Returns("/"); httpContext .Setup(hc => hc.Response.Cookies) .Returns(responseCookies); // Act tempDataProvider.SaveTempData(httpContext.Object, values); // Assert Assert.Equal(1, responseCookies.Count); var cookieInfo = responseCookies[CookieTempDataProvider.CookieName]; Assert.NotNull(cookieInfo); Assert.Equal(expectedDataInCookie, cookieInfo.Value); Assert.Equal(expectedDataToProtect, dataProtector.PlainTextToProtect); }
private static bool ValidateProperty(TempDataSerializer tempDataSerializer, List <string> errorMessages, PropertyInfo property) { if (!(property.SetMethod != null && property.SetMethod.IsPublic && property.GetMethod != null && property.GetMethod.IsPublic)) { errorMessages.Add( Resources.FormatTempDataProperties_PublicGetterSetter(property.DeclaringType.FullName, property.Name, nameof(TempDataAttribute))); return(false); } if (!tempDataSerializer.CanSerializeType(property.PropertyType)) { var errorMessage = Resources.FormatTempDataProperties_InvalidType( tempDataSerializer.GetType().FullName, TypeNameHelper.GetTypeDisplayName(property.DeclaringType), property.Name, TypeNameHelper.GetTypeDisplayName(property.PropertyType)); errorMessages.Add(errorMessage); return(false); } return(true); }
public void LoadTempData_Base64UrlDecodesAnd_UnprotectsData_FromCookie() { // Arrange var expectedValues = new Dictionary <string, object>(); expectedValues.Add("int", 10); var tempDataProviderSerializer = new TempDataSerializer(); var expectedDataToUnprotect = tempDataProviderSerializer.Serialize(expectedValues); var base64AndUrlEncodedDataInCookie = WebEncoders.Base64UrlEncode(expectedDataToUnprotect); var dataProtector = new PassThroughDataProtector(); var tempDataProvider = GetProvider(dataProtector); var requestCookies = new RequestCookieCollection(new Dictionary <string, string>() { { CookieTempDataProvider.CookieName, base64AndUrlEncodedDataInCookie } }); var httpContext = new Mock <HttpContext>(); httpContext .Setup(hc => hc.Request.Cookies) .Returns(requestCookies); // Act var actualValues = tempDataProvider.LoadTempData(httpContext.Object); // Assert Assert.Equal(expectedDataToUnprotect, dataProtector.DataToUnprotect); Assert.Equal(expectedValues, actualValues); }
public CookieTempDataProvider(IDataProtectionProvider dataProtectionProvider, IOptions <CookieTempDataProviderOptions> options) { _dataProtector = dataProtectionProvider.CreateProtector(Purpose); _tempDataSerializer = new TempDataSerializer(); _chunkingCookieManager = new ChunkingCookieManager(); _options = options; }
public CookieTempDataProvider( IDataProtectionProvider dataProtectionProvider, ILoggerFactory loggerFactory, IOptions <CookieTempDataProviderOptions> options) { _dataProtector = dataProtectionProvider.CreateProtector(Purpose); _logger = loggerFactory.CreateLogger <CookieTempDataProvider>(); _tempDataSerializer = new TempDataSerializer(); _chunkingCookieManager = new ChunkingCookieManager(); _options = options.Value; }
public void SaveTempData_CustomProviderOptions_SetsCookie_WithAppropriateCookieOptions( string requestPathBase, string optionsPath, string optionsDomain, string expectedCookiePath, string expectedDomain) { // Arrange var values = new Dictionary <string, object>(); values.Add("int", 10); var tempDataProviderStore = new TempDataSerializer(); var expectedDataToProtect = tempDataProviderStore.Serialize(values); var expectedDataInCookie = WebEncoders.Base64UrlEncode(expectedDataToProtect); var dataProtector = new PassThroughDataProtector(); var tempDataProvider = GetProvider( dataProtector, new CookieTempDataProviderOptions { Cookie = { Path = optionsPath, Domain = optionsDomain } }); var responseCookies = new MockResponseCookieCollection(); var httpContext = new Mock <HttpContext>(); httpContext .SetupGet(hc => hc.Request.IsHttps) .Returns(false); httpContext .SetupGet(hc => hc.Request.PathBase) .Returns(requestPathBase); httpContext .Setup(hc => hc.Response.Cookies) .Returns(responseCookies); // Act tempDataProvider.SaveTempData(httpContext.Object, values); // Assert Assert.Equal(1, responseCookies.Count); var cookieInfo = responseCookies[CookieTempDataProvider.CookieName]; Assert.NotNull(cookieInfo); Assert.Equal(expectedDataInCookie, cookieInfo.Value); Assert.Equal(expectedDataToProtect, dataProtector.PlainTextToProtect); Assert.Equal(expectedCookiePath, cookieInfo.Options.Path); Assert.Equal(expectedDomain, cookieInfo.Options.Domain); Assert.False(cookieInfo.Options.Secure); Assert.True(cookieInfo.Options.HttpOnly); Assert.Null(cookieInfo.Options.Expires); }
public MultitenantCookieTempDataProvider( IDataProtectionProvider dataProtectionProvider, ILoggerFactory loggerFactory , TempDataSerializer tempDataSerializer ) { _dataProtectionProvider = dataProtectionProvider; _loggerFactory = loggerFactory; _tempDataSerializer = tempDataSerializer; }
public void SaveTempData_HonorsCookieSecurePolicy_OnOptions( bool isRequestSecure, CookieSecurePolicy cookieSecurePolicy, bool expectedSecureFlag) { // Arrange var values = new Dictionary <string, object>(); values.Add("int", 10); var tempDataProviderStore = new TempDataSerializer(); var expectedDataToProtect = tempDataProviderStore.Serialize(values); var expectedDataInCookie = WebEncoders.Base64UrlEncode(expectedDataToProtect); var dataProtector = new PassThroughDataProtector(); var options = new CookieTempDataProviderOptions(); options.Cookie.SecurePolicy = cookieSecurePolicy; var tempDataProvider = GetProvider(dataProtector, options); var responseCookies = new MockResponseCookieCollection(); var httpContext = new Mock <HttpContext>(); httpContext .SetupGet(hc => hc.Request.PathBase) .Returns("/"); httpContext .SetupGet(hc => hc.Request.IsHttps) .Returns(isRequestSecure); httpContext .Setup(hc => hc.Response.Cookies) .Returns(responseCookies); // Act tempDataProvider.SaveTempData(httpContext.Object, values); // Assert Assert.Equal(1, responseCookies.Count); var cookieInfo = responseCookies[CookieTempDataProvider.CookieName]; Assert.NotNull(cookieInfo); Assert.Equal(expectedDataInCookie, cookieInfo.Value); Assert.Equal(expectedDataToProtect, dataProtector.PlainTextToProtect); Assert.Equal("/", cookieInfo.Options.Path); Assert.Equal(expectedSecureFlag, cookieInfo.Options.Secure); Assert.True(cookieInfo.Options.HttpOnly); Assert.Null(cookieInfo.Options.Expires); Assert.Null(cookieInfo.Options.Domain); }
public CompositeTempDataProvider( IDataProtectionProvider dataProtectionProvider, IOptions <CookieTempDataProviderOptions> options, TempDataSerializer tempDataSerializer, ILoggerFactory loggerFactory) { _dataProtector = dataProtectionProvider.CreateProtector(Purpose); _tempDataSerializer = tempDataSerializer; _chunkingCookieManager = new ChunkingCookieManager(); _logger = loggerFactory.CreateLogger <CookieTempDataProvider>(); _options = options.Value; _cookieName = options.Value.Cookie.Name; _tempDataCookieLoadFailure = LoggerMessage.Define <string>( LogLevel.Warning, new EventId(3, "TempDataCookieLoadFailure"), "The temp data cookie {CookieName} could not be loaded."); }
public void SaveTempData_RemovesCookie_WhenNoDataToSave() { // Arrange var values = new Dictionary <string, object>(); values.Add("int", 10); var tempDataProviderStore = new TempDataSerializer(); var serializedData = tempDataProviderStore.Serialize(values); var base64AndUrlEncodedData = WebEncoders.Base64UrlEncode(serializedData); var dataProtector = new PassThroughDataProtector(); var tempDataProvider = GetProvider(dataProtector); var requestCookies = new RequestCookieCollection(new Dictionary <string, string>() { { CookieTempDataProvider.CookieName, base64AndUrlEncodedData } }); var responseCookies = new MockResponseCookieCollection(); var httpContext = new Mock <HttpContext>(); httpContext .SetupGet(hc => hc.Request.PathBase) .Returns("/"); httpContext .Setup(hc => hc.Request.Cookies) .Returns(requestCookies); httpContext .Setup(hc => hc.Response.Cookies) .Returns(responseCookies); httpContext .Setup(hc => hc.Response.Headers) .Returns(new HeaderDictionary()); // Act tempDataProvider.SaveTempData(httpContext.Object, new Dictionary <string, object>()); // Assert Assert.Equal(1, responseCookies.Count); var cookie = responseCookies[CookieTempDataProvider.CookieName]; Assert.NotNull(cookie); Assert.Equal(string.Empty, cookie.Value); Assert.NotNull(cookie.Options.Expires); Assert.True(cookie.Options.Expires.Value < DateTimeOffset.Now); // expired cookie }
public void SaveTempData_SetsSecureAttributeOnCookie_OnlyIfRequestIsSecure(bool isSecure) { // Arrange var values = new Dictionary <string, object>(); values.Add("int", 10); var tempDataProviderStore = new TempDataSerializer(); var expectedDataToProtect = tempDataProviderStore.Serialize(values); var expectedDataInCookie = Base64UrlTextEncoder.Encode(expectedDataToProtect); var dataProtector = new PassThroughDataProtector(); var tempDataProvider = GetProvider(dataProtector); var responseCookies = new MockResponseCookieCollection(); var httpContext = new Mock <HttpContext>(); httpContext .SetupGet(hc => hc.Request.PathBase) .Returns("/"); httpContext .SetupGet(hc => hc.Request.IsHttps) .Returns(isSecure); httpContext .Setup(hc => hc.Response.Cookies) .Returns(responseCookies); // Act tempDataProvider.SaveTempData(httpContext.Object, values); // Assert Assert.Equal(1, responseCookies.Count); var cookieInfo = responseCookies[CookieTempDataProvider.CookieName]; Assert.NotNull(cookieInfo); Assert.Equal(expectedDataInCookie, cookieInfo.Value); Assert.Equal(expectedDataToProtect, dataProtector.PlainTextToProtect); Assert.Equal("/", cookieInfo.Options.Path); Assert.Equal(isSecure, cookieInfo.Options.Secure); Assert.True(cookieInfo.Options.HttpOnly); Assert.Null(cookieInfo.Options.Expires); Assert.Null(cookieInfo.Options.Domain); }
public static IReadOnlyList <LifecycleProperty> GetTempDataProperties( TempDataSerializer tempDataSerializer, Type type) { List <LifecycleProperty> results = null; var errorMessages = new List <string>(); var propertyHelpers = PropertyHelper.GetVisibleProperties(type: type); for (var i = 0; i < propertyHelpers.Length; i++) { var propertyHelper = propertyHelpers[i]; var property = propertyHelper.Property; var tempDataAttribute = property.GetCustomAttribute <TempDataAttribute>(); if (tempDataAttribute != null && ValidateProperty(tempDataSerializer, errorMessages, propertyHelper.Property)) { if (results == null) { results = new List <LifecycleProperty>(); } var key = tempDataAttribute.Key; if (string.IsNullOrEmpty(key)) { key = property.Name; } results.Add(new LifecycleProperty(property, key)); } } if (errorMessages.Count > 0) { throw new InvalidOperationException(string.Join(Environment.NewLine, errorMessages)); } return(results); }
public void LoadTempData_ReturnsEmptyDictionary_AndClearsCookie_WhenDataIsInvalid() { // Arrange var dataProtector = new Mock <IDataProtector>(MockBehavior.Strict); dataProtector .Setup(d => d.Unprotect(It.IsAny <byte[]>())) .Throws(new Exception()); var tempDataProvider = GetProvider(dataProtector.Object); var inputData = new Dictionary <string, object>(); inputData.Add("int", 10); var tempDataProviderSerializer = new TempDataSerializer(); var expectedDataToUnprotect = tempDataProviderSerializer.Serialize(inputData); var base64AndUrlEncodedDataInCookie = WebEncoders.Base64UrlEncode(expectedDataToUnprotect); var context = new DefaultHttpContext(); context.Request.Cookies = new RequestCookieCollection(new Dictionary <string, string>() { { CookieTempDataProvider.CookieName, base64AndUrlEncodedDataInCookie } }); // Act var tempDataDictionary = tempDataProvider.LoadTempData(context); // Assert Assert.Empty(tempDataDictionary); var setCookieHeader = SetCookieHeaderValue.Parse(context.Response.Headers["Set-Cookie"].ToString()); Assert.Equal(CookieTempDataProvider.CookieName, setCookieHeader.Name.ToString()); Assert.Equal(string.Empty, setCookieHeader.Value.ToString()); }
public void SaveTempData_UsesCookieName_FromOptions() { // Arrange var expectedCookieName = "TestCookieName"; var values = new Dictionary <string, object>(); values.Add("int", 10); var tempDataProviderStore = new TempDataSerializer(); var expectedDataToProtect = tempDataProviderStore.Serialize(values); var expectedDataInCookie = WebEncoders.Base64UrlEncode(expectedDataToProtect); var tempDataProvider = GetProvider(dataProtector: null, options: new CookieTempDataProviderOptions() { Cookie = { Name = expectedCookieName } }); var responseCookies = new MockResponseCookieCollection(); var httpContext = new Mock <HttpContext>(); 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 == expectedCookieName); var cookieInfo = responseCookies[expectedCookieName]; Assert.Equal(expectedDataInCookie, cookieInfo.Value); Assert.Equal("/", cookieInfo.Options.Path); }
public TempDataFilterPageApplicationModelProvider(TempDataSerializer tempDataSerializer) { _tempDataSerializer = tempDataSerializer; }
/// <summary> /// Initializes a new instance of <see cref="SessionStateTempDataProvider"/>. /// </summary> /// <param name="tempDataSerializer">The <see cref="TempDataSerializer"/>.</param> public SessionStateTempDataProvider(TempDataSerializer tempDataSerializer) { _tempDataSerializer = tempDataSerializer; }
public SessionStateTempDataProvider() { _tempDataSerializer = new TempDataSerializer(); }