2

I need some help with .NET Core 3.1 for code that needs to get out of the corporate proxy. The code works in .NET 4.7.2 by putting the following in the app.config. This, I learned on this site (Thank you!), allows one to get through the corporate proxy server.

 <system.net>
    <defaultProxy useDefaultCredentials="true">
    </defaultProxy>
  </system.net>

The following code snippet works in .NET 4.7.2, and can get out past the proxy. The proxy is stored as an env variable named ALL_PROXY with a value of http:// our internal proxy:port (see [https://github.com/dotnet/corefx/pull/37238/commits/9ba8879ea104afac9dea9a78d3009b5bc700b7c3][1]). This is an Azure Cognitive service, so you will need a key vault, and a Cognitive Service.

  var keyVaultName = Environment.GetEnvironmentVariable("KEY_VAULT_NAME");
  var kvUri = string.Format("{0}{1}{2}", 
                "https://", 
                keyVaultName, 
                ".vault.azure.net");
  var secretClient = new SecretClient(new Uri(kvUri), 
                new DefaultAzureCredential());
  KeyVaultSecret secret = secretClient.GetSecret("FacesSubscription");
  var subscriptionKey = secret.Value;

  var credentials = new ApiKeyServiceClientCredentials(subscriptionKey);
  var textClient = new TextAnalyticsClient(credentials)
  {
      Endpoint = endpoint
   };

On .Net core, I get an http 407 exception. Azure.RequestFailedException: 'Service request failed. Status: 407 (ADAuth-AuthenticationFailed) This exception occurs on the KeyVaultSecret line.

I have researched the issue @

and tried adding code such as

var handler = new HttpClientHandler();
handler.DefaultProxyCredentials = CredentialCache.DefaultCredentials;
var httpClient = new HttpClient(handler);

I looked through the unit tests for the check-ins, but they all seemed to be about web proxies. The links above provide details of how the proxy is implemented.

Does anyone have idea around getting through a work proxy in NET Core with Azure services?

3
  • Did you find a solution for this? Commented Jul 13, 2020 at 11:35
  • Not yet- the project is on hold and the proxy team is making changes internally. Commented Jul 15, 2020 at 11:21
  • 1
    this is how I managed to get it working in my case stackoverflow.com/questions/64931470/… @Tom Commented Nov 21, 2020 at 13:32

1 Answer 1

3

I have had a similar issue and this fixed it:

IWebProxy proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = CredentialCache.DefaultCredentials;
HttpClient.DefaultProxy = proxy;
Sign up to request clarification or add additional context in comments.

1 Comment

Where did you put this in your code? I am assuming it would be best practice to put it in the startup / configuration before any requests go out?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.