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?
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.
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");
}
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With