[英]Web Service with Windows Authentication with Soap Client
我需要从ac#forms app访问web服务。
Web服务需要Windows身份验证。
我使用以下代码:
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身份验证,而不是匿名?
在客户端的app.config中的<binding>部分中添加以下内容:
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Ntlm" proxyCredentialType="None" realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
(来自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");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.