Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Service with Windows Authentication with Soap Client

I need to access a webservice from a c# forms app.

The webservice needs Windows Authentication.

I use the following code:

ServiceDeskSoapClient sd = new ServiceDeskSoapClient();
sd.ClientCredentials.UserName.UserName = @"mydomain\myusername";
sd.ClientCredentials.UserName.Password = "mypassword";
sd.MyMethod();

But get the following error:

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

How do I correctly set the credentials so it uses windows auth, not anonymous?

like image 817
Ben Avatar asked Oct 22 '11 22:10

Ben


People also ask

How do you provide authentication for SOAP Web services?

The basic authentication is encoded in the HTTP request that carries the SOAP message. When the application server receives the HTTP request, the user name and password are retrieved and verified using the authentication mechanism specific to the server. Use transport-level security to enable basic authentication.


2 Answers

In case anyone still needs this, I had the pleasure of figuring it out today. It really was quite simple:

var server = new MySoapClient();
if (server.ClientCredentials != null)
{
    server.ClientCredentials.Windows.AllowNtlm = true;
    server.ClientCredentials.Windows.ClientCredential = new NetworkCredential("MyUsername", "MyPassword", "MyDomain");
}
like image 105
teh4x Avatar answered Oct 15 '22 13:10

teh4x


Add the following inside the <binding> section in the client's app.config:

<security mode="TransportCredentialOnly">
  <transport clientCredentialType="Ntlm" proxyCredentialType="None" realm="" />
  <message clientCredentialType="UserName" algorithmSuite="Default" />
</security>

(from http://morrisbahrami.blogspot.com.au/2011/02/http-request-is-unauthorized-with.html)

like image 23
Seb Wills Avatar answered Oct 15 '22 13:10

Seb Wills