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?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Tom
  • 45
  • 1
  • 9
  • Did you find a solution for this? – NickG Jul 13 '20 at 11:35
  • Not yet- the project is on hold and the proxy team is making changes internally. – Tom Jul 15 '20 at 11:21
  • 1
    this is how I managed to get it working in my case https://stackoverflow.com/questions/64931470/how-to-set-default-proxy-with-net-core-3-1-for-http-client-for-any-request @Tom – Ali Hussain Nov 21 '20 at 13:32

1 Answers1

3

I have had a similar issue and this fixed it:

IWebProxy proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = CredentialCache.DefaultCredentials;
HttpClient.DefaultProxy = proxy;
  • 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? – ajw170 Apr 27 '23 at 03:07