Skip to content

Commit

Permalink
fix: Allow using default credentials for proxy in .Net (box/box-cod…
Browse files Browse the repository at this point in the history
  • Loading branch information
box-sdk-build committed Dec 4, 2024
1 parent 55718dc commit c4e5fc1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .codegen.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "engineHash": "6a4806e", "specHash": "544d370", "version": "1.4.0" }
{ "engineHash": "a839036", "specHash": "544d370", "version": "1.4.0" }
21 changes: 15 additions & 6 deletions Box.Sdk.Gen/Networking/Fetch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,24 @@ private static HttpClient GetOrCreateHttpClient(NetworkSession networkSession)

private static HttpClient CreateProxyClient(ProxyConfig proxyConfig)
{
var webProxy = new WebProxy(proxyConfig.Url)
{
UseDefaultCredentials = proxyConfig.UseDefaultCredentials
};

if (!proxyConfig.UseDefaultCredentials)
{
webProxy.Credentials = new NetworkCredential(
proxyConfig.Username,
proxyConfig.Password,
proxyConfig.Domain);
}

var handler = new HttpClientHandler
{
Proxy = new WebProxy(proxyConfig.Url)
{
Credentials = new NetworkCredential(proxyConfig.Username, proxyConfig.Password, proxyConfig.Domain)
},
Proxy = webProxy,
UseProxy = true,
PreAuthenticate = true,
UseDefaultCredentials = false
PreAuthenticate = true
};

return new HttpClient(handler, disposeHandler: true);
Expand Down
5 changes: 4 additions & 1 deletion Box.Sdk.Gen/Networking/ProxyConfig/ProxyConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ public class ProxyConfig {

public string? Domain { get; init; }

public ProxyConfig(string url) {
public bool UseDefaultCredentials { get; }

public ProxyConfig(string url, bool useDefaultCredentials = false) {
Url = url;
UseDefaultCredentials = useDefaultCredentials;
}
}
}
11 changes: 10 additions & 1 deletion docs/Client.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,17 @@ var newClient = client.WithCustomBaseUrls(new BaseUrls(

# Use Proxy for API calls

In order to use a proxy for API calls, calling the `client.WithProxy(proxyConfig)` method creates a new client, leaving the original client unmodified, with the username and password being optional.
In order to use a proxy for API calls, call the `client.WithProxy(proxyConfig)` method that creates a new client with proxy, leaving the original client unmodified.
In config, you can specify the username, password and domain for the proxy server.
Alternatively you can set 'UseDefaultCredentials' to true to use the credentials of the currently logged on user - `DefaultCredentials`.
NOTE: Setting UseDefaultCredentials takes precedence over Username, Password and Domain fields. If UseDefaultCredentials is set to true, the Username, Password and Domain fields will be ignored.

```c#
var newClient = client.WithProxy(new ProxyConfig("http://proxy.com") { Username = "username", Password = "password", Domain = "example" });
```

To use proxy with default credentials:

```c#
var newClient = client.WithProxy(new ProxyConfig("http://proxy.com") { UseDefaultCredentials = true });
```

0 comments on commit c4e5fc1

Please sign in to comment.