.netproxywindows-authenticationcredentialsflurl

.NET Core 8 - Flurl not using the current user credentials for the default proxy


I am building a .NET Core 8 app that sends an HTTP request to a REST API using Flurl.

When I manually set the proxy with my credentials, it works fine. However, when I don't set my credentials, the request uses the default system proxy settings, but doesn't uses the current user credentials.

Note: launching a cURL from the command line with the same user works just fine.

This is the code I am using:

// Uncomment that to make it work :
//FlurlHttp.Clients.WithDefaults( builder => builder
//  .ConfigureInnerHandler( hch => {
//      hch.PreAuthenticate = true;
//      // Current user credentials required :
//      hch.Proxy = new System.Net.WebProxy( "http://my-proxy:myport", true, null, new NetworkCredential( "mydomain\\mylogin", "mypassword" ) );
//      hch.UseProxy = true;
//  } ) );

var response = await "https://the-oauth-server/oauth/token"
    .PostUrlEncodedAsync( new {
        client_id = "my-client-id",
        client_secret = "my-client-secret",
        grant_type = "client_credentials",
        scope = "the-scope"
    } );
// Without the manual proxy configuration (with the current user credentials !)
// Exception : The proxy tunnel request to proxy 'http://my-proxy:myport/' failed with status code '407'

I tried to set the proxy like this :

hch.Proxy = System.Net.WebRequest.GetSystemWebProxy();

And tried to set the credentials like this :

hch.Proxy.Credentials = CredentialCache.DefaultCredentials;

Solution

  • You just need to set UseDefaultCredentials on the WebProxy to true:

    hch.Proxy = new System.Net.WebProxy("http://my-proxy:myport", true)
    {
        UseDefaultCredentials = true
    };
    hch.UseProxy = true;