.net 从 wcf 客户端调用需要基本 http 身份验证的 Web 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3495903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling a web service that requires basic http authentication from wcf client
提问by Vladimir Georgiev
I have a wsdl from a web service, I generated the wcf proxy. No problem.
我有一个来自 Web 服务的 wsdl,我生成了 wcf 代理。没问题。
But I can not get my head around how to pass the user name and password. The webservice requires basic authentication - only username and password.
但是我无法理解如何传递用户名和密码。Web 服务需要基本身份验证 - 只有用户名和密码。
Any help ?
有什么帮助吗?
回答by Ladislav Mrnka
Is Basic authentication configured in configuration file? Do you need to pass only credentials or do you also need secured transport (HTTPS)?
配置文件中是否配置了基本身份验证?您只需要传递凭据还是还需要安全传输 (HTTPS)?
First you need to set up binding to support Basic authentication
首先你需要设置绑定以支持基本认证
Setup for HTTP binding:
HTTP 绑定的设置:
<bindings>
<basicHttpBinding>
<binding name="BasicAuth">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic" />
</security>
</binding>
</basicHttpBinding>
</bindings>
Setup for HTTPS binding:
HTTPS 绑定设置:
<bindings>
<basicHttpBinding>
<binding name="BasicAuthSecured">
<security mode="Transport">
<transport clientCredentialType="Basic" />
</security>
</binding>
</basicHttpBinding>
</bindings>
Client endpoint has to use defined configuration like:
客户端端点必须使用定义的配置,如:
<client>
<endpoint address="..."
name="..."
binding="basicHttpBinding"
bindingConfiguration="BasicAuth"
contract="..." />
</client>
Then you have to pass credentials to the proxy:
然后您必须将凭据传递给代理:
proxy = new MyServiceClient();
proxy.ClientCredentials.UserName.UserName = "...";
proxy.ClientCredentials.UserName.Password = "...";
回答by Peladao
This should cover it: http://msdn.microsoft.com/en-us/library/ms733775.aspx(See the Client section)
这应该涵盖它:http: //msdn.microsoft.com/en-us/library/ms733775.aspx(请参阅客户端部分)
回答by Jagmag
I would say it is likely to depend on how the web service expects you to pass the information. After all, you are only the consumer.
我会说这可能取决于 Web 服务希望您如何传递信息。毕竟,你只是消费者。
Having said that, it is common is web services to have the userid and password passed in the SOAP Header.
话虽如此,Web 服务在 SOAP Header 中传递用户 ID 和密码是很常见的。
You can refer to this linkfor a sample implementation of this scenario
您可以参考此链接以获取此场景的示例实现
Sample Soap Message
示例肥皂消息
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<AUTHHEADER xmlns="http://tempuri.org/">
<USERNAME>string</USERNAME>
<PASSWORD>string</PASSWORD>
</AUTHHEADER>
</soap:Header>
<soap:Body>
<SENSITIVEDATA xmlns="http://tempuri.org/" />
</soap:Body>
</soap:Envelope>

