使用Soap Client进行Windows身份validation的Web服务
我需要从ac#forms app访问web服务。
Web服务需要Windows身份validation。
我使用以下代码:
ServiceDeskSoapClient sd = new ServiceDeskSoapClient(); sd.ClientCredentials.UserName.UserName = @"mydomain\myusername"; sd.ClientCredentials.UserName.Password = "mypassword"; sd.MyMethod();
但是得到以下错误:
The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Negotiate,NTLM'.
如何正确设置凭据,以便它使用Windows身份validation,而不是匿名?
在客户端的app.config中的
(来自http://morrisbahrami.blogspot.com.au/2011/02/http-request-is-unauthorized-with.html )
如果有人仍然需要这个,我很高兴今天搞清楚。 这真的很简单:
var server = new MySoapClient(); if (server.ClientCredentials != null) { server.ClientCredentials.Windows.AllowNtlm = true; server.ClientCredentials.Windows.ClientCredential = new NetworkCredential("MyUsername", "MyPassword", "MyDomain"); }