mono http requests behind proxy - c#

I've written a C# app that performs web requests via the System.Net.Http.HttpClient (e.g. client.GetAsync(uri);). Compiling and executing it with the .Net runtime, all these calls are successful. However, compiling and running with Mono, they fail with exceptions ("IOException Authentication or decryption failed").
However, switching to a network that doesn't go through the proxy resolves the issue. So in conclusion, it's not a certificate or whatever issue, but just the issue of the proxy.
Same applies for the tlstest tool: Fails miserably with the proxy, works fine without it.
How do I configure mono to use the proxy settings / use the system proxy settings?

Can you try setting the proxy in HttpClient? You can set proxy credentials if necessary.
WebProxy proxy = new WebProxy("proxyaddress", port);
HttpClientHandler handler = new HttpClientHandler()
{
Proxy = proxy
};
HttpClient client = new HttpClient(handler);

Try to use modernhttpclient component. This library brings the latest platform-specific networking libraries to Xamarin applications via a custom HttpClient handler which can handle for you this kind of issues.

Related

Injecting proxy credentials to all HttpClient requests

I'm working on a corporate ASP.NET Core application that needs to reach out to a cloud resource (Elastic APM).
Unfortunately, our corporate proxy is swatting down the request before it can complete. It's not my code that making the requests but code within a NuGet package, so I can't easily change how it's making the connection.
I'm hoping to use middleware to inject the proxy details before it goes out the door. I've tried this below, but it doesn't seem to be working:
services.AddHttpClient<HttpClient>().ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler()
{
Proxy = new WebProxy(new Uri("http://proxy.mycompany.com:8080"), true, new string[]{}, CredentialCache.DefaultCredentials)
});
Long-term, I am going to whitelist the cloud resource. But as I prototype this solution, I'd prefer not to go through that red tape...

How to properly establish SSL connection from UWP App to WebAPI using HttpClient class?

I have an UWP application which communicates with server WebAPI. To do this I am using HttpClient class from Windows.Web.Http namespace. It does work fine if I use simple Http URL. Because of sending passwords and other sesitive data I would like to use SSL. But the problem appears when I am trying to use Https URL. Program still shows me an error which says that "The certificate authority is invalid or incorrect". I am new in certificates etc. so it does not tell me anything useful.
On some websites I found some semi-solution. Using HttpBaseProtocolFilter class I can set options to ignore some server certificate errors like this:
HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();
filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted);
HttpClient client = new HttpClient(filter);
It works, but only on desktop. When I run my application on Mobile Emulator, the error shows up again. Also I do not really think that this is a good solution so I have searched for something else and just found this: https://pfelix.wordpress.com/2012/12/16/using-httpclient-with-ssltls/ . There are some informations about HttpClient and SSL, but as far as I understand it is about HttpClient from namespace System.Net.Http. I tried this solution for Windows Store App, but it have not worked too.
var client = new HttpClient(new HttpClientHandler
{
ClientCertificateOptions = ClientCertificateOption.Automatic
});
So here is my question: how can I set up this HttpClient (from Windows.Web.Http or System.Net.Http) to perform working SSL connection with my WebAPI? Maybe is there something to set by server side?
Edit: I am using IIS Express from Visual Studio 2015 because I do not really know how to add my WebAPI to full IIS and let it work. I also turn on SSL in WebAPI project Properties, which generated for me a Https URL (so I thought that it would also generate a properly certificate). If I try to navigate to the Http URL everything is fine on my Mozilla Firefox and IE. The error about untrusted certificate shows when I try my https://localhost:44301/ URL in both browsers.
You were close with the HttpClientHandler approach, but you want to trust the server certificate, not the client certificate (which doesn't exist in your example). Try this:
HttpClient client = new HttpClient(new HttpClientHandler {
ServerCertificateCustomValidationCallback = (a, b, c, d) => true });
This will cause ALL server certificates to be trusted so don't use it in production, but works great for accessing an https service on localhost from a UWP app during development.

UWP client using HttpClient to communicate with .NET Web API, failed connection when SSL is enabled

I'm doing a project where I need to communicate with a Web API from a UWP-application.
I use HttpClient to do the job, and it works fine as long as the Web API is not using SSL. When I turn the SSL on, it just won't work. It works if I use some other client though, like browser/Wpf-application/console-application.
Probably you need to allow unsigned / self signed certificates. If so, you can do it the following way
var filter = new HttpBaseProtocolFilter();
filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted);
var httpClient = new Windows.Web.Http.HttpClient(filter);
I hope it helps

Method not found when setting proxy in HttpClientHandler Portable Class Library (PCL)

I'm using Microsoft HTTP Client Libraries - PCL (installed via NuGet) to communicate with a public REST API. Because I'm behind a corporate firewall, I need to configure the proxy info on the HttpClientHandler.
However, when setting proxy on the HttpClientHandler, it throws the following error:
Method not found: 'Void System.Net.Http.HttpClientHandler.set_Proxy(System.Net.IWebProxy)'.
There's nothing special about my code, so I am a bit puzzled:
var handler = new HttpClientHandler
{
UseDefaultCredentials = false,
Proxy = new DefaultProxy
{
Credentials =
new NetworkCredential(
"firstname.lastname",
"P4ssw0rd",
"DOMAIN")
},
UseProxy = true
};
this.client = new HttpClient(handler);
Some notes:
I've successfully used the HttpClientHandler from the standard .Net library (i.e. not PCL) for another project, so I know the proxy information is correct.
I've successfully retrieved the data when the proxy is bypassed (i.e. outside the corporate network, or no proxy).
DefaultProxy implements IWebProxy.
This error is due to the HttpClient package not being installed on the consuming project. You should be getting a warning similar:
All projects referencing [consuming project] must install nuget package Microsoft.Bcl.Build.
Basically, every referencing a class library that references HttpClient & Async must also reference HttpClient & Async.

Using WCF service in MonoTouch with Authentication

I am using a WCF service client generated by slsvcutil form Silverlight toolkit version 4. I've also tried version 3 with the same problems. When I use a client instance running on http with no user credentials it runs without problems. But I need to switch to https for productive servers and send user credentials that are hardcoded for my application. I use the following code for that:
var binding = new BasicHttpBinding (BasicHttpSecurityMode.TransportCredentialOnly);
var endpoint = new EndpointAddress (AppSettings.FlareEndPoint);
_service = new TopicAnalystAPIClient(binding, endpoint);
_service.ClientCredentials.UserName.UserName = "xxx";
_service.ClientCredentials.UserName.Password = "xxx";
When I call a method on that service pointing to http with no authentication it works. When I use the this code against http/https with the credential I get "There was an error on processing web request: Status code 401(Unauthorized): Unauthorized" exception. I've checked that the credentials are correct, I am able to open the service reference in my browser. I've also tried several combinations of http/https and SecurityMode value. I've also tried it on four different servers always with the same result.
What can be the problem?
A lot of permutations are possible. BasicHttpSecurityMode.TransportCredentialOnly should be usable without SSL [1] using HTTP itself. This means the server will send one (or more) authentication method(s) to the client (e.g. basic, digest, ntlm) and Mono (including MonoTouch) should be providing support for the most of them.
It is possible that the linker (if used) removes one of them. In that case you could try building and testing without linking (or skip linking of System.Net.dll).
It's also possible that the authentication method that the serve insist on is not supported. You could find which one is used by running a network trace (e.g. wireshark) or, maybe, it will show up in more details in the server log (along with the 401 error).
[1] http://msdn.microsoft.com/en-us/library/system.servicemodel.basichttpsecuritymode%28v=vs.95%29.aspx

Categories