[英]Web Service with Windows Authentication with Soap Client
I need to access a webservice from ac# forms app. 我需要从ac#forms app访问web服务。
The webservice needs Windows Authentication. Web服务需要Windows身份验证。
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? 如何正确设置凭据,以便它使用Windows身份验证,而不是匿名?
Add the following inside the <binding> section in the client's app.config: 在客户端的app.config中的<binding>部分中添加以下内容:
<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 ) (来自http://morrisbahrami.blogspot.com.au/2011/02/http-request-is-unauthorized-with.html )
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");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.