Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,29 @@ internal static void AddHeaders(IEnumerable<string> headersToForward, HttpReques
if (headersToForward.Any())
{
headerNames = headerNames.Where(headersToForward.Contains);
}

foreach (var headerName in headerNames)
{
// Workaround for an issue identified by https://github.com/dotnet/systemweb-adapters/issues/228.
// HttpClient wrongly uses comma (",") instead of semi-colon (";") as a separator for Cookie headers.
// To mitigate this, we concatenate them manually and put them back as a single header value.
// This workaround can be removed once we target .NET 7+ as Kestrel is fixed there.
if (string.Equals(headerName, HeaderNames.Cookie, StringComparison.OrdinalIgnoreCase))
{
authRequest.Headers.Add(headerName, string.Join("; ", originalRequest.Headers[headerName].ToArray()));
continue;
}

authRequest.Headers.Add(headerName, originalRequest.Headers[headerName].ToArray());
}

foreach (var headerName in headerNames)
{
var originalHeaders = (IEnumerable<string>)originalRequest.Headers[headerName];

// Workaround for an issue identified by https://github.com/dotnet/systemweb-adapters/issues/228.
// HttpClient wrongly uses comma (",") instead of semi-colon (";") as a separator for Cookie headers.
// To mitigate this, we concatenate them manually and put them back as a single header value.
// This workaround can be removed once we target .NET 7+ as Kestrel is fixed there.
if (string.Equals(headerName, HeaderNames.Cookie, StringComparison.OrdinalIgnoreCase))
{
authRequest.Headers.Add(headerName, string.Join("; ", originalHeaders));
continue;
}

// Workaround for an issue when adding an empty Authorization header
if (string.Equals(headerName, HeaderNames.Authorization, StringComparison.OrdinalIgnoreCase))
{
originalHeaders = originalHeaders.Where(x => !string.IsNullOrEmpty(x));
}

authRequest.Headers.Add(headerName, originalHeaders);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ public static IEnumerable<object[]> GetHeadersForConcatenation()
{
new Dictionary<string, StringValues>(){ { "Cookie", new StringValues(new[] { "1", "2" }) } },
new Dictionary<string, StringValues>(){ { "Cookie", new StringValues(new[] { "1; 2" }) } }
};

// Authorization header with a token
yield return new object[]
{
new Dictionary<string, StringValues>(){ { "Authorization", new StringValues("TOKEN") } },
new Dictionary<string, StringValues>(){ { "Authorization", new StringValues("TOKEN") } }
};

// Empty authorization header
yield return new object[]
{
new Dictionary<string, StringValues>(){ { "Authorization", new StringValues(string.Empty) } },
new Dictionary<string, StringValues>()
};
}
}