Runtime: How to sent Network Credentials to Proxy in .NET Core app while calling a web service?

Created on 11 Jul 2019  路  8Comments  路  Source: dotnet/runtime

I am using .NET Core 2.2 app.
Trying to connect to Web Service. Request goes through proxy server.

I am getting the following error:
ERROR- The remote server returned an unexpected response: (407) Proxy Authorization Required.

But I don't have this issue to talk to same service using .NET App.

Looks like .NET Core 2.2 has bug? it is supposed to pass default Network Credentials, similar to .NET App or it should allow me to set the credentials.
Any thoughts on this issue?

area-System.Net.Http question

Most helpful comment

The same works, if I use .NET Framework client and with the config file setting:

.NET Core does not have global config files/settings.

However, we did add a new API for .NET Core 3.0.

```c#
HttpClient.DefaultProxy // static property


This allows you to do something like this which replaces the global config setting with .NET Framework <system.net> config.

```c#
HttpClient.DefaultProxy.Credentials = CredentialCache.DefaultCredentials;

See: PR dotnet/corefx#37333 and Issue ##36553 for discussion.

All 8 comments

Looks like .NET Core 2.2 has bug? it is supposed to pass default Network Credentials, similar to .NET App or it should allow me to set the credentials.

You need to write code like this:

```c#
var handler = new HttpClientHandler();
handler.DefaultProxyCredentials = CredentialCache.DefaultCredentials;

var client = new HttpClient(handler);
```

Then use HttpClient APIs such as GetAsync(), etc. This will make sure that default network credentials are sent to a system configured proxy server.

I am actually referring the Web Service in my .NET Core project, using Connected Services option( adding service references in project)
If I have REST API, then I could have followed the above approach mentioned by you.
my case is similar to WCF client calling a WCF service. so here instead of WCF client, I am using .NET Core. This works fine when no proxy configured. But fails with 407 error when proxy is configured.
similar issue is being discussed here:
https://forums.asp.net/t/2131534.aspx?Problem+when+connecting+to+a+SOAP+service+with+Net+Core+2+0+behind+a+proxy
But i guess no answer yet.

But fails with 407 error when proxy is configured.
similar issue is being discussed here:

In general, credentials are never "automatically" sent to either a proxy server or a destination server. Thus, you will always get a 407 or 401 if you don't pass credentials.

For enterprise scenarios, many proxy servers use Windows authentication schemes like Negotiate or NTLM. So, the credential to be passed to the proxy could be the "current" logged on user. In which case, the username/password isn't specified directly but instead is passed as CredentialCache.DefaultCredentials.

But in any case, the underlying HTTP APIs like HttpWebRequest or HttpClient need to be configured in order to set those credentials. So, it sounds like your code isn't doing that.

Yes, but I am using proxy class generated by the "Microsoft WCF Web Service Reference Provider". There is no API/Property exposed for met to set the CredentialCache.DefaultCredentials.
The same works, if I use .NET Framework client and with the config file setting:

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

But the same is not possible with .NET Core Client. I was hoping that, atleast they provide an API to set the CredentialCache.DefaultCredentials. But I could not find any such API. Looks like .NET Core team forgot this? or Am I missing something?

The same works, if I use .NET Framework client and with the config file setting:

.NET Core does not have global config files/settings.

However, we did add a new API for .NET Core 3.0.

```c#
HttpClient.DefaultProxy // static property


This allows you to do something like this which replaces the global config setting with .NET Framework <system.net> config.

```c#
HttpClient.DefaultProxy.Credentials = CredentialCache.DefaultCredentials;

See: PR dotnet/corefx#37333 and Issue ##36553 for discussion.

Thank you for understanding the issue.
So, In the .NET Core 2.2
I will not be able to make web service calls, which requires proxy credentials.
Proxy throws 407 error always as .NET Core 2.2 has no mecahsnim to send proxy credentials while making web service calls

I will wait for .NET Core 3.0 for this issue to be addressed.
Or please let me know if you have any way forward or workaround with .NET Core 2.2

Thanks

It looks like the issue here is not so much how to supply proxy info to HttpClient, but to the WCF Connected Service.

I found an issue 3551 on the WCF repo for this.

Closing this issue. Please track https://github.com/dotnet/wcf/issues/3551

Was this page helpful?
0 / 5 - 0 ratings