question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NullReferenceException when using proxy server with NTLM authentication and BasicHttpBinding (... and potentially others)

See original GitHub issue

When using BasicHttpBinding (or potentially others) behind a web proxy that requires authentication, the authenticationScheme cannot be used, as when it comes to opening the http channel, a NullReferenceException is raised in System.Private.ServiceModel.dll when trying to use an uninialized class member.

Root cause: the credential manager only gets instantiated when using authentication against the service itsself but the proxy authentication, which actually can be different (and is in most cases in large enterprise scenarios) is being ignored, even if passed by the used binding.

Code to reproduce:

 private Binding CreateBinding()
 {
           BasicHttpBinding binding = new BasicHttpBinding();
            if (this.ServiceUri.OriginalString.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
            {
                binding.Security.Mode = BasicHttpSecurityMode.Transport;
            }
            else
            {
                binding.Security.Mode = BasicHttpSecurityMode.None;
            }
            binding.CloseTimeout = TimeSpan.FromSeconds(2);
            binding.OpenTimeout = TimeSpan.FromSeconds(5);
            binding.ReceiveTimeout = TimeSpan.FromMinutes(2);
            binding.SendTimeout = TimeSpan.FromMinutes(2);
            binding.MaxBufferSize = Int32.MaxValue;
            binding.MaxReceivedMessageSize = Int32.MaxValue;
            binding.AllowCookies = true;


            CustomBinding customBinding = new CustomBinding(binding);
            if (this.IsProxyEnabled)
            {
                HttpTransportBindingElement httpElement = customBinding.Elements.Find<HttpTransportBindingElement>();
                httpElement.UseDefaultWebProxy = false;
                httpElement.ProxyAddress = new Uri($"http://{this.ServiceSettings.ProxyHostname}:{this.ServiceSettings.ProxyPort}");
                httpElement.ProxyAuthenticationScheme = AuthenticationSchemes.Ntlm; // produces NullReferenceException in WCF client (master) during execution
                //httpElement.AuthenticationScheme = AuthenticationSchemes.Ntlm; // nicht richtig - das hier wäre für die authentifizierung am Webservice selbst
                httpElement.BypassProxyOnLocal = false;
            }
            return customBinding;
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dimalyshevcommented, Mar 12, 2021

@mconnew, sorry in advance for mess with this 2 prev PR - I really don’t know how to purge these artifacts from issue history. But #4563 really fixes issue with Proxy with Basic auth and gets relief with all this nightmares about configuring binding proxy.

The exception call stack for #4562 is here:

System.InvalidOperationException: The required UserNameSecurityToken was not provided.
   at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result) in \wcf\System.Private.ServiceModel\src\Internals\System\Runtime\AsyncResult.cs:line 354
   at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result) in \wcf\System.Private.ServiceModel\src\System\ServiceModel\Channels\ServiceChannel.cs:line 1909
   at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result) in \wcf\System.Private.ServiceModel\src\System\ServiceModel\Channels\ServiceChannel.cs:line 806
   at System.ServiceModel.Channels.ServiceChannelProxy.TaskCreator.<>c__DisplayClass1_0.<CreateGenericTask>b__0(IAsyncResult asyncResult) in \wcf\System.Private.ServiceModel\src\System\ServiceModel\Channels\ServiceChannelProxy.cs:line 184
--- End of stack trace from previous location where exception was thrown ---
1reaction
dimalyshevcommented, Mar 12, 2021

httpElement.ProxyAuthenticationScheme = AuthenticationSchemes.Basic; // produce UsernameSecurityToken error

Read more comments on GitHub >

github_iconTop Results From Across the Web

System.InvalidOperationException: The required ...
NullReferenceException when using proxy server with NTLM authentication and BasicHttpBinding (... and potentially others ) #4557.
Read more >
c# - How to setup proxy Credentials using ...
I coded a WCF Service using HttpTransportBindingElement in conjunction with IIS on port 80. The code works fine as long as no proxy...
Read more >
active directory - NTLM through proxy server
NTLM is connection orientated and since there's no direct connection between you and the webserver when you use the proxy, so NTLM fails....
Read more >
Can't consume web services via an HTTP proxy server
This article provides a resolution to fix the error that occurs on a .NET client that consumes a Web service via an HTTP...
Read more >
All client browsers repeatedly asking for NTLM ...
This is repeatable with some clients running through my proxy server, but in other cases it does not happen at all. In the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found