我正在尝试调用SOAP Web服务,但是我收到错误: 附加信息:未提供用户名。在ClientCredentials中指定用户名。
所以我想我可以将client.ClientCredentials设置为NetworkCredentials的新实例。但ClientCredentials是只读的。那么我怎样才能将这些信息传递给访问Web服务?
myService.ServiceClient client = new myService.ServiceClient();
// This won't work since its read only.
client.ClientCredentials = new System.Net.NetworkCredential("username", "password", "domain");
string version = client.getData();
编辑: 结合:
<binding name="VersionHttpBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic" />
</security>
</binding>
答案 0 :(得分:29)
您需要在客户端设置凭据,如图所示in this MSDN article:
client.ClientCredentials.UserName.UserName = "my_user_name";
client.ClientCredentials.UserName.Password = "my_password";