Search code examples
c#webproxy

Authentication failed because remote party has closed the transport stream, need solution


Our VPC of company using an http system proxy. Due to the fact, there are no https proxy and any web links should using http proxy even it is a https.

I tried to add these two solutions to my C# code, but it doesn't help.

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

or

ServicePointManager.ServerCertificateValidationCallback =  delegate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
{ 
   return true; 
};

Some Details.

I also write a python code by O365 graph api python.

Solution is quiet simple.

Change connection.py source code from

if proxy_server and proxy_port:
            if proxy_username and proxy_password:
                self.proxy = {
                    "http": "http://{}:{}@{}:{}".format(proxy_username,
                                                        proxy_password,
                                                        proxy_server,
                                                        proxy_port),
                    "https": "https://{}:{}@{}:{}".format(proxy_username,
                                                          proxy_password,
                                                          proxy_server,
                                                          proxy_port),
                }
            else:
                self.proxy = {
                    "http": "http://{}:{}".format(proxy_server, proxy_port),
                    "https": "https://{}:{}".format(proxy_server, proxy_port),
                }

to

if proxy_server and proxy_port:
            if proxy_username and proxy_password:
                self.proxy = {
                    "http": "http://{}:{}@{}:{}".format(proxy_username,
                                                        proxy_password,
                                                        proxy_server,
                                                        proxy_port),
                    "https": "http://{}:{}@{}:{}".format(proxy_username,
                                                          proxy_password,
                                                          proxy_server,
                                                          proxy_port),
                }
            else:
                self.proxy = {
                    "http": "http://{}:{}".format(proxy_server, proxy_port),
                    "https": "http://{}:{}".format(proxy_server, proxy_port),
                }

System works.


Solution

  • For .NET Framework 4.x:

    • If your program code runs on .NET Framework 4.x...
    • ...and if your program uses HttpClient, HttpWebRequest, or even the horrible WebClient class without any further trickery (e.g. custom HttpMessageHandler classes that do HTTPS-to-HTTP conversion themselves, or other attempts at implementing proxy logic)
    • then you can specify a HTTP proxy in your app.config (or web.config if your application runs within IIS) with the <defaultProxy> element and one-or-more child <proxy /> elements:
    <configuration>
    
        <system.net>
            <defaultProxy enabled="true" useDefaultCredentials="false">
                <proxy autoDetect="false" bypassOnLocal="false"  proxyAddress="http://your-proxy-server:1234" useSystemDefault="false" />
            </defaultProxy>
        </system.net>
    
    </configuration>
    

    For .NET Core 3.0 or later, .NET 5, and .NET 6, and later:

    • .NET Core 3.0 introduced HttpClient.DefaultProxy to allow applications to gain custom HTTP proxy logic by implementing System.Net.IWebProxy themselves and and passing a reference into the static IWebProxy DefaultProxy { get; set; } property in HttpClient (ideally only once, during application startup).
    • For (zero-code) configuration you cannot use <defaultProxy> in app.config as .NET Framework's traditional XML-based configuration feature was removed, instead you can configure process-wide HTTP/HTTPS proxy settings by setting any of these environment variables: HTTP_PROXY, HTTPS_PROXY, ALL_PROXY, and NO_PROXY.
      • These environment variables are is documented on the same page as HttpClient.DefaultProxy.
      • Using those environment-variables will cause System.Net.WebProxy to be pre-configured and passed into HttpClient.WebProxy already for you.