我使用Visual Studio 2008创建了一个Web服务代理,它为我创建了app.config中的以下条目:

<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>

最后我可以使用默认的basicHttpBinding。与问题中发布的代码的唯一区别是安全节点

另请注意 mode =&quot; TransportCredentialOnly&quot; 选项,这允许您使用 http 而不是 https 发送用户名/密码。这对于我正在使用的测试环境是必要的。稍后您显然会更喜欢 https 来发送您的凭据。

然后在代码中输入您的用户名/密码:

var ws = new ***.MyHandlerClient("MyHandler");
ws.ClientCredentials.UserName.UserName = "myUsername";
ws.ClientCredentials.UserName.Password = "myPassword";
var result = ws.executeMyMethod();

其他提示

错误消息是正确的。 WCF不允许通过不受保护的协议传输用户名和密码。您的网络服务必须使用HTTPS(附带SSL证书)

获得SSL证书后,您有两种选择,包括如何发送凭据,传输或安全性以及凭证类型的多个选项。 MSDN对所有各种选项都有良好指南

我遇到了同样的问题并尝试了上面的解决方案 不知怎的,这对我不起作用 我一直收到消息“找不到WS-Security标头”

经过长时间的测试和尝试后,我设法让它发挥作用 我在客户端添加了头代码,如下所示,然后就可以了!

<client>
    <endpoint address="http://your.service.com" binding="basicHttpBinding" bindingConfiguration="XXXBinding" contract="contract.XXX" name="XXXPort">
        <headers xmlns:wsse="http://your.xsd">
            <wsse:Security mustUnderstand="1">
                <wsse:UsernameToken>
                    <tenant>XXX</tenant>
                    <wsse:Username>XXX</wsse:Username>
                    <wsse:Password Type="http://www.xxxx.com/wss#PasswordText">XXX</wsse:Password>
                </wsse:UsernameToken>
            </wsse:Security>
        </headers>
    </endpoint>
</client>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top