我使用Visual Studio 2008创建了Web服务的代理,并为我创建了app.config中的以下条目: system.serviceModel bindings basicHttpBinding binding name="MyNameHandlerSoapBinding" closeTimeout="00:01:00" openTimeout="00:01:00"
<system.serviceModel> <bindings> <basicHttpBinding> <binding name="MyNameHandlerSoapBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <security mode="None"> <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> <message clientCredentialType="UserName" algorithmSuite="Default" /> </security> </binding> </basicHttpBinding> </bindings> <client> <endpoint address="http://www.***/***/***" binding="basicHttpBinding" bindingConfiguration="MyNameHandlerSoapBinding" contract="***.MyNameHandler" name="MyName"> </endpoint> </client> </system.serviceModel>
webservice有用户名/密码认证,所以我需要在这里添加。
我有点迷失在WCF文档的大海里,我想我必须从basicHttpBinding更改为wsHttpBinding或customBinding才能添加身份验证元素,但我不太明白。任何人都可以提供任何快速提示或任何有用的链接,说明如何做?
编辑:
我将安全部分更改为:
<security mode="Transport"> <transport clientCredentialType="Basic" proxyCredentialType="None" realm="" /> </security>
并加入代码:
ws.ClientCredentials.UserName.UserName = ""; ws.ClientCredentials.UserName.Password = "";
现在看来它可能正在使用凭据,但它给我的错误:
提供的URI方案’http’是无效的URI预期’https’
我甚至不知道这是否是正确的出路?
我在这里发布解决方案供未来读者:<system.serviceModel> <bindings> <basicHttpBinding> <binding name="MyHandlerSoapBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <security mode="TransportCredentialOnly"> <transport clientCredentialType="Basic" /> </security> </binding> </basicHttpBinding> </bindings> <client> <endpoint address="http://www.***/***/***/MyHandler" binding="basicHttpBinding" bindingConfiguration="MyHandlerSoapBinding" contract="***.MyHandler" name="MyHandler"> </endpoint> </client> </system.serviceModel>
最后我可以使用默认的basicHttpBinging。与问题中发布的代码的唯一区别是安全性节点。
还要注意mode =“TransportCredentialOnly”选项,这样可以使用http而不是https发送用户名/密码。这是我正在使用的测试环境所必需的。后来显然你会更愿意https发送你的凭据。
之后,您将输入您的用户名/密码:
var ws = new ***.MyHandlerClient("MyHandler"); ws.ClientCredentials.UserName.UserName = "myUsername"; ws.ClientCredentials.UserName.Password = "myPassword"; var result = ws.executeMyMethod();