使用Soap Client进行Windows身份验证的Web服务

时间:2011-10-22 22:08:03

标签: c# wcf web-services

我需要从c#表单应用程序访问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身份验证,而不是匿名?

2 个答案:

答案 0 :(得分:7)

如果有人还需要这个,我很高兴今天能够搞清楚这一点。这真的很简单:

var server = new MySoapClient();
if (server.ClientCredentials != null)
{
    server.ClientCredentials.Windows.AllowNtlm = true;
    server.ClientCredentials.Windows.ClientCredential = new NetworkCredential("MyUsername", "MyPassword", "MyDomain");
}

答案 1 :(得分:7)

在< binding>内添加以下内容客户端的app.config中的部分:

<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