-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Explain how to configure a proxy server via code
- Loading branch information
1 parent
043b3c1
commit 08821e7
Showing
5 changed files
with
100 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--- | ||
title: Configure a proxy server when using the Azure SDK for .NET | ||
description: Learn different ways to configure a proxy server for use with the Azure SDK for .NET client libraries. | ||
ms.topic: conceptual | ||
ms.custom: devx-track-dotnet, engagement-fy23 | ||
ms.date: 11/15/2024 | ||
--- | ||
|
||
# Configure a proxy server when using the Azure SDK for .NET | ||
|
||
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. | ||
|
||
## Configure using code | ||
|
||
To programmatically configure a proxy for use in underlying HTTP operations supporting the Azure SDK for .NET libraries, complete the following steps: | ||
|
||
1. Create an <xref:System.Net.Http.HttpClientHandler> object whose `Proxy` property is set. | ||
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. | ||
1. Pass the service client options object to the service client constructor. | ||
|
||
Using the Azure Key Vault Secrets library as an example, you'd have the following code: | ||
|
||
:::code language="csharp" source="snippets/configure-proxy/Program.cs"::: | ||
|
||
## Configure using environment variables | ||
|
||
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. | ||
|
||
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. | ||
|
||
### [cmd](#tab/cmd) | ||
|
||
```cmd | ||
rem Non-authenticated HTTP server: | ||
set HTTP_PROXY=http://10.10.1.10:1180 | ||
rem Authenticated HTTP server: | ||
set HTTP_PROXY=http://username:[email protected]:1180 | ||
rem Non-authenticated HTTPS server: | ||
set HTTPS_PROXY=http://10.10.1.10:1180 | ||
rem Authenticated HTTPS server: | ||
set HTTPS_PROXY=http://username:[email protected]:1180 | ||
``` | ||
|
||
### [bash](#tab/bash) | ||
|
||
```bash | ||
# Non-authenticated HTTP server: | ||
HTTP_PROXY=http://10.10.1.10:1180 | ||
|
||
# Authenticated HTTP server: | ||
HTTP_PROXY=http://username:[email protected]:1180 | ||
|
||
# Non-authenticated HTTPS server: | ||
HTTPS_PROXY=http://10.10.1.10:1180 | ||
|
||
# Authenticated HTTPS server: | ||
HTTPS_PROXY=http://username:[email protected]:1180 | ||
``` | ||
|
||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Net; | ||
using Azure.Core.Pipeline; | ||
using Azure.Identity; | ||
using Azure.Security.KeyVault.Secrets; | ||
|
||
using HttpClientHandler handler = new() | ||
{ | ||
Proxy = new WebProxy(new Uri("<http://example.com>")), | ||
}; | ||
|
||
SecretClientOptions options = new() | ||
{ | ||
Transport = new HttpClientTransport(handler), | ||
}; | ||
SecretClient client = new( | ||
new Uri("https://<key-vault-name>.vault.azure.net/"), | ||
new DefaultAzureCredential(), | ||
options); |
15 changes: 15 additions & 0 deletions
15
docs/azure/sdk/snippets/configure-proxy/ProxyServerConfiguration.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Azure.Identity" Version="1.13.1" /> | ||
<PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.7.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |