Skip to content

Commit 08821e7

Browse files
committed
Explain how to configure a proxy server via code
1 parent 043b3c1 commit 08821e7

File tree

5 files changed

+100
-51
lines changed

5 files changed

+100
-51
lines changed

.openpublishing.redirection.azure.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@
119119
{
120120
"source_path_from_root": "/docs/azure/ai/get-started-app-chat-scaling-with-azure-container-apps.md",
121121
"redirect_url": "/dotnet/ai/get-started-app-chat-scaling-with-azure-container-apps"
122+
},
123+
{
124+
"source_path_from_root": "/docs/azure/sdk/azure-sdk-configure-proxy.md",
125+
"redirect_url": "/dotnet/azure/sdk/configure-proxy"
122126
}
123127
]
124128
}

docs/azure/sdk/azure-sdk-configure-proxy.md

Lines changed: 0 additions & 51 deletions
This file was deleted.

docs/azure/sdk/configure-proxy.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
title: Configure a proxy server when using the Azure SDK for .NET
3+
description: Learn different ways to configure a proxy server for use with the Azure SDK for .NET client libraries.
4+
ms.topic: conceptual
5+
ms.custom: devx-track-dotnet, engagement-fy23
6+
ms.date: 11/15/2024
7+
---
8+
9+
# Configure a proxy server when using the Azure SDK for .NET
10+
11+
If your organization requires the use of a proxy server to access Internet resources, some configuration is required to use the Azure SDK for .NET client libraries. Once configured, the proxy is applied to the `HttpClient` instance used for HTTP operations.
12+
13+
## Configure using code
14+
15+
To programmatically configure a proxy for use in underlying HTTP operations supporting the Azure SDK for .NET libraries, complete the following steps:
16+
17+
1. Create an <xref:System.Net.Http.HttpClientHandler> object whose `Proxy` property is set.
18+
1. Create a service client options object whose <xref:Azure.Core.ClientOptions.Transport%2A> property is set to an `HttpClientTransport` object accepting the `HttpClientHandler` instance.
19+
1. Pass the service client options object to the service client constructor.
20+
21+
Using the Azure Key Vault Secrets library as an example, you'd have the following code:
22+
23+
:::code language="csharp" source="snippets/configure-proxy/Program.cs":::
24+
25+
## Configure using environment variables
26+
27+
Depending on if your proxy server uses HTTP or HTTPS, you'll set either the environment variable `HTTP_PROXY` or `HTTPS_PROXY`, respectively. The proxy server URL takes the form `http[s]://[username:password@]<ip_address_or_hostname>:<port>/`, where the `username:password` combination is optional. To get the IP address or hostname, port, and credentials for your proxy server, consult your network administrator.
28+
29+
The following examples show how to set the appropriate environment variables in command shell (Windows) and bash (Linux/macOS) environments. Setting the appropriate environment variable causes the Azure SDK for .NET libraries to use the proxy server at runtime.
30+
31+
### [cmd](#tab/cmd)
32+
33+
```cmd
34+
rem Non-authenticated HTTP server:
35+
set HTTP_PROXY=http://10.10.1.10:1180
36+
37+
rem Authenticated HTTP server:
38+
set HTTP_PROXY=http://username:[email protected]:1180
39+
40+
rem Non-authenticated HTTPS server:
41+
set HTTPS_PROXY=http://10.10.1.10:1180
42+
43+
rem Authenticated HTTPS server:
44+
set HTTPS_PROXY=http://username:[email protected]:1180
45+
```
46+
47+
### [bash](#tab/bash)
48+
49+
```bash
50+
# Non-authenticated HTTP server:
51+
HTTP_PROXY=http://10.10.1.10:1180
52+
53+
# Authenticated HTTP server:
54+
HTTP_PROXY=http://username:[email protected]:1180
55+
56+
# Non-authenticated HTTPS server:
57+
HTTPS_PROXY=http://10.10.1.10:1180
58+
59+
# Authenticated HTTPS server:
60+
HTTPS_PROXY=http://username:[email protected]:1180
61+
```
62+
63+
---
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Net;
2+
using Azure.Core.Pipeline;
3+
using Azure.Identity;
4+
using Azure.Security.KeyVault.Secrets;
5+
6+
using HttpClientHandler handler = new()
7+
{
8+
Proxy = new WebProxy(new Uri("<http://example.com>")),
9+
};
10+
11+
SecretClientOptions options = new()
12+
{
13+
Transport = new HttpClientTransport(handler),
14+
};
15+
SecretClient client = new(
16+
new Uri("https://<key-vault-name>.vault.azure.net/"),
17+
new DefaultAzureCredential(),
18+
options);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Azure.Identity" Version="1.13.1" />
12+
<PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.7.0" />
13+
</ItemGroup>
14+
15+
</Project>

0 commit comments

Comments
 (0)