Dave
Dave

Reputation: 2180

SOAP web service call with NTLM auth not working C#

I'm trying to do a SOAP web service call using NTLM authentication but it doesn't work.

I used the WSDL service.

What I did so far:

BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("http://uri.test/");
_client = new TEST_PortClient(binding, address);

if (_client.ClientCredentials != null)
{
  _client.ClientCredentials.Windows.AllowNtlm = true; // this method is deprecated
  _client.ClientCredentials.Windows.ClientCredential.UserName = "username";
  _client.ClientCredentials.Windows.ClientCredential.Password = "password";
}
_client.Open(); // this works successfully
string message = string.Empty;
if (_client.TestConnection(ref message)) // this throw an exception *
{
  // do something
}

The exception thrown is:

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'NTLM'.

What should I do to make it works?

Upvotes: 1

Views: 5081

Answers (1)

Dave
Dave

Reputation: 2180

I solved it in this way:

BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress(Uri);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;

binding.MessageEncoding = WSMessageEncoding.Text;
binding.TextEncoding = Encoding.UTF8;
binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;

_client = new TEST_PortClient(binding, address);

if (_client.ClientCredentials != null)
{
  _client.ClientCredentials.Windows.ClientCredential = new NetworkCredential("username", "password");
  _client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Delegation;

}
_client.Open();

string message = string.Empty;
if (_client.TestConnection(ref message))
{
  // do something
}

Upvotes: 9

Related Questions