/// <inheritdoc /> public void Append(string key, string value) { var setCookieHeaderValue = new SetCookieHeaderValue( Uri.EscapeDataString(key), Uri.EscapeDataString(value)) { Path = "/" }; string cookieValue; if (_builderPool == null) { cookieValue = setCookieHeaderValue.ToString(); } else { var stringBuilder = _builderPool.Get(); try { setCookieHeaderValue.AppendToStringBuilder(stringBuilder); cookieValue = stringBuilder.ToString(); } finally { _builderPool.Return(stringBuilder); } } Headers[HeaderNames.SetCookie] = StringValues.Concat(Headers[HeaderNames.SetCookie], cookieValue); }
/// <summary> /// Add a new cookie and value /// </summary> /// <param name="key"></param> /// <param name="value"></param> public void Append(string key, string value) { var setCookieHeaderValue = new SetCookieHeaderValue( UrlEncoder.Default.Encode(key), UrlEncoder.Default.Encode(value)) { Path = "/" }; Headers[HeaderNames.SetCookie] = StringValues.Concat(Headers[HeaderNames.SetCookie], setCookieHeaderValue.ToString()); }
/// <summary> /// Add a new cookie /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="options"></param> public void Append(string key, string value, CookieOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } var setCookieHeaderValue = new SetCookieHeaderValue( UrlEncoder.Default.Encode(key), UrlEncoder.Default.Encode(value)) { Domain = options.Domain, Path = options.Path, Expires = options.Expires, Secure = options.Secure, HttpOnly = options.HttpOnly, }; Headers[HeaderNames.SetCookie] = StringValues.Concat(Headers[HeaderNames.SetCookie], setCookieHeaderValue.ToString()); }
public void SetCookieHeaderValue_ToString_SameSiteNoneCompat() { SetCookieHeaderValue.SuppressSameSiteNone = true; var input = new SetCookieHeaderValue("name", "value") { SameSite = SameSiteMode.None, }; Assert.Equal("name=value", input.ToString()); SetCookieHeaderValue.SuppressSameSiteNone = false; var input2 = new SetCookieHeaderValue("name", "value") { SameSite = SameSiteMode.None, }; Assert.Equal("name=value; samesite=none", input2.ToString()); }
public void SetCookieHeaderValue_ToString(SetCookieHeaderValue input, string expectedValue) { Assert.Equal(expectedValue, input.ToString()); }
public void SetCookieHeaderValue_ToString(SetCookieHeaderValue input, string expectedValue) { Assert.Equal(expectedValue, input.ToString()); }
/// <summary> /// Appends a new response cookie to the Set-Cookie header. If the cookie is larger than the given size limit /// then it will be broken down into multiple cookies as follows: /// Set-Cookie: CookieName=chunks-3; path=/ /// Set-Cookie: CookieNameC1=Segment1; path=/ /// Set-Cookie: CookieNameC2=Segment2; path=/ /// Set-Cookie: CookieNameC3=Segment3; path=/ /// </summary> /// <param name="context"></param> /// <param name="key"></param> /// <param name="value"></param> /// <param name="options"></param> public void AppendResponseCookie(HttpContext context, string key, string value, CookieOptions options) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (key == null) { throw new ArgumentNullException(nameof(key)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } var template = new SetCookieHeaderValue(key) { Domain = options.Domain, Expires = options.Expires, HttpOnly = options.HttpOnly, Path = options.Path, Secure = options.Secure, }; var templateLength = template.ToString().Length; value = value ?? string.Empty; // Normal cookie var responseCookies = context.Response.Cookies; if (!ChunkSize.HasValue || ChunkSize.Value > templateLength + value.Length) { responseCookies.Append(key, value, options); } else if (ChunkSize.Value < templateLength + 10) { // 10 is the minimum data we want to put in an individual cookie, including the cookie chunk identifier "CXX". // No room for data, we can't chunk the options and name throw new InvalidOperationException(Resources.Exception_CookieLimitTooSmall); } else { // Break the cookie down into multiple cookies. // Key = CookieName, value = "Segment1Segment2Segment2" // Set-Cookie: CookieName=chunks-3; path=/ // Set-Cookie: CookieNameC1="Segment1"; path=/ // Set-Cookie: CookieNameC2="Segment2"; path=/ // Set-Cookie: CookieNameC3="Segment3"; path=/ var dataSizePerCookie = ChunkSize.Value - templateLength - 3; // Budget 3 chars for the chunkid. var cookieChunkCount = (int)Math.Ceiling(value.Length * 1.0 / dataSizePerCookie); responseCookies.Append(key, ChunkCountPrefix + cookieChunkCount.ToString(CultureInfo.InvariantCulture), options); var offset = 0; for (var chunkId = 1; chunkId <= cookieChunkCount; chunkId++) { var remainingLength = value.Length - offset; var length = Math.Min(dataSizePerCookie, remainingLength); var segment = value.Substring(offset, length); offset += length; responseCookies.Append(key + ChunkKeySuffix + chunkId.ToString(CultureInfo.InvariantCulture), segment, options); } } }
/// <summary> /// Appends a new response cookie to the Set-Cookie header. If the cookie is larger than the given size limit /// then it will be broken down into multiple cookies as follows: /// Set-Cookie: CookieName=chunks:3; path=/ /// Set-Cookie: CookieNameC1=Segment1; path=/ /// Set-Cookie: CookieNameC2=Segment2; path=/ /// Set-Cookie: CookieNameC3=Segment3; path=/ /// </summary> /// <param name="context"></param> /// <param name="key"></param> /// <param name="value"></param> /// <param name="options"></param> public void AppendResponseCookie(HttpContext context, string key, string value, CookieOptions options) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (key == null) { throw new ArgumentNullException(nameof(key)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } var escapedKey = Encoder.UrlEncode(key); var template = new SetCookieHeaderValue(escapedKey) { Domain = options.Domain, Expires = options.Expires, HttpOnly = options.HttpOnly, Path = options.Path, Secure = options.Secure, }; var templateLength = template.ToString().Length; value = value ?? string.Empty; var quoted = false; if (IsQuoted(value)) { quoted = true; value = RemoveQuotes(value); } var escapedValue = Encoder.UrlEncode(value); // Normal cookie var responseHeaders = context.Response.Headers; if (!ChunkSize.HasValue || ChunkSize.Value > templateLength + escapedValue.Length + (quoted ? 2 : 0)) { template.Value = quoted ? Quote(escapedValue) : escapedValue; responseHeaders.Append(Constants.Headers.SetCookie, template.ToString()); } else if (ChunkSize.Value < templateLength + (quoted ? 2 : 0) + 10) { // 10 is the minimum data we want to put in an individual cookie, including the cookie chunk identifier "CXX". // No room for data, we can't chunk the options and name throw new InvalidOperationException(Resources.Exception_CookieLimitTooSmall); } else { // Break the cookie down into multiple cookies. // Key = CookieName, value = "Segment1Segment2Segment2" // Set-Cookie: CookieName=chunks:3; path=/ // Set-Cookie: CookieNameC1="Segment1"; path=/ // Set-Cookie: CookieNameC2="Segment2"; path=/ // Set-Cookie: CookieNameC3="Segment3"; path=/ var dataSizePerCookie = ChunkSize.Value - templateLength - (quoted ? 2 : 0) - 3; // Budget 3 chars for the chunkid. var cookieChunkCount = (int)Math.Ceiling(escapedValue.Length * 1.0 / dataSizePerCookie); template.Value = "chunks:" + cookieChunkCount.ToString(CultureInfo.InvariantCulture); responseHeaders.Append(Constants.Headers.SetCookie, template.ToString()); var chunks = new string[cookieChunkCount]; var offset = 0; for (var chunkId = 1; chunkId <= cookieChunkCount; chunkId++) { var remainingLength = escapedValue.Length - offset; var length = Math.Min(dataSizePerCookie, remainingLength); var segment = escapedValue.Substring(offset, length); offset += length; template.Name = escapedKey + "C" + chunkId.ToString(CultureInfo.InvariantCulture); template.Value = quoted ? Quote(segment) : segment; chunks[chunkId - 1] = template.ToString(); } responseHeaders.Append(Constants.Headers.SetCookie, chunks); } }
/// <inheritdoc /> public void Append(string key, string value, CookieOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } var setCookieHeaderValue = new SetCookieHeaderValue( Uri.EscapeDataString(key), Uri.EscapeDataString(value)) { Domain = options.Domain, Path = options.Path, Expires = options.Expires, Secure = options.Secure, HttpOnly = options.HttpOnly, }; string cookieValue; if (_builderPool == null) { cookieValue = setCookieHeaderValue.ToString(); } else { var stringBuilder = _builderPool.Get(); try { setCookieHeaderValue.AppendToStringBuilder(stringBuilder); cookieValue = stringBuilder.ToString(); } finally { _builderPool.Return(stringBuilder); } } Headers[HeaderNames.SetCookie] = StringValues.Concat(Headers[HeaderNames.SetCookie], cookieValue); }