使用NTLM身份验证的SOAP Web服务调用不起作用C#

时间:2019-03-29 11:45:41

标签: c# soap wsdl

我正在尝试使用NTLM身份验证进行SOAP Web服务调用,但是它不起作用。

我使用了WSDL服务。

我到目前为止所做的:

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 HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'NTLM'.

我应该怎么做才能使其起作用?

1 个答案:

答案 0 :(得分:0)

我以这种方式解决了:

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
}