Skip to content

Commit a52dec4

Browse files
Copilotphilnach
andcommitted
Add HttpClient with default credentials for authenticated proxy support
Co-authored-by: philnach <[email protected]>
1 parent 64ecff6 commit a52dec4

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

Extensions/Cosmos/Cosmos.DataTransfer.CosmosExtension/CosmosExtensionServices.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,24 @@
99
using Microsoft.Azure.Cosmos.Encryption;
1010
using Azure.Security.KeyVault.Keys.Cryptography;
1111
using System.Net;
12+
using System.Net.Http;
1213

1314
namespace Cosmos.DataTransfer.CosmosExtension
1415
{
1516
public static class CosmosExtensionServices
1617
{
18+
// Static HttpClient with default credentials for reuse across connections
19+
// This avoids connection exhaustion and properly handles credentials
20+
private static readonly Lazy<HttpClient> _httpClientWithDefaultCredentials = new Lazy<HttpClient>(() =>
21+
{
22+
var handler = new HttpClientHandler
23+
{
24+
Credentials = CredentialCache.DefaultNetworkCredentials,
25+
PreAuthenticate = true
26+
};
27+
return new HttpClient(handler);
28+
});
29+
1730
public static CosmosClient CreateClient(CosmosSettingsBase settings, string displayName, string? sourceDisplayName = null)
1831
{
1932
string userAgentString = CreateUserAgentString(displayName, sourceDisplayName);
@@ -44,6 +57,13 @@ public static CosmosClient CreateClient(CosmosSettingsBase settings, string disp
4457
}
4558
clientOptions.WebProxy = webProxy;
4659
}
60+
61+
// When using default credentials, also configure the HttpClient with credentials
62+
// This ensures authenticated proxy support for the underlying HTTP connections
63+
if (settings.UseDefaultProxyCredentials)
64+
{
65+
clientOptions.HttpClientFactory = () => _httpClientWithDefaultCredentials.Value;
66+
}
4767

4868
CosmosClient? cosmosClient;
4969
if (settings.UseRbacAuth)

Extensions/Cosmos/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Source supports the following optional parameters:
2121
- `PartitionKeyValue` - Allows for filtering to a single partition.
2222
- `Query` - Allows further filtering using a Cosmos SQL statement.
2323
- `WebProxy` (`null` by default) - Enables connections through a proxy.
24-
- `UseDefaultProxyCredentials` (`false` by default) - When `true`, includes default credentials in the proxy request. Use this when connecting through an authenticated proxy that returns [`407 Proxy Authentication Required`](https://learn.microsoft.com/dotnet/api/system.net.webproxy.credentials?view=net-10.0#remarks).
24+
- `UseDefaultProxyCredentials` (`false` by default) - When `true`, includes default credentials in both the WebProxy and the underlying HttpClient connection to CosmosDB. Use this when connecting through an authenticated proxy that returns [`407 Proxy Authentication Required`](https://learn.microsoft.com/dotnet/api/system.net.webproxy.credentials?view=net-10.0#remarks).
2525

2626
### Always Encrypted
2727

@@ -93,7 +93,7 @@ Or with RBAC:
9393
- `Direct`
9494

9595
- **`WebProxy`**: Optional. Specifies the proxy server URL to use for connections (e.g., `http://yourproxy.server.com/`).
96-
- **`UseDefaultProxyCredentials`**: Optional, defaults to `false`. When `true`, includes default credentials in the proxy request. Use this when connecting through an authenticated proxy that returns [`407 Proxy Authentication Required`](https://learn.microsoft.com/dotnet/api/system.net.webproxy.credentials?view=net-10.0#remarks).
96+
- **`UseDefaultProxyCredentials`**: Optional, defaults to `false`. When `true`, includes default credentials in both the WebProxy and the underlying HttpClient connection to CosmosDB. Use this when connecting through an authenticated proxy that returns [`407 Proxy Authentication Required`](https://learn.microsoft.com/dotnet/api/system.net.webproxy.credentials?view=net-10.0#remarks).
9797

9898
- **`LimitToEndpoint`**: Optional, defaults to `false`. When the value of this property is false, the Cosmos DB SDK will automatically discover
9999
write and read regions, and use them when the configured application region is not available.

0 commit comments

Comments
 (0)