与WCF连接到使用用户名/密码验证的WebService

ant*_*ioh 19 wcf wcf-authentication wcf-security

我使用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>
Run Code Online (Sandbox Code Playgroud)

webservice有用户名/密码验证,所以我需要在这里添加它.

我在WCF文档的海洋中有点迷失,我想我必须从basicHttpBinding更改为wsHttpBinding或customBinding才能添加身份验证元素,但我真的不明白它.任何人都可以提供任何快速提示或任何有用的链接,说明如何做到这一点?

编辑:

我将安全部分更改为:

<security mode="Transport">
    <transport clientCredentialType="Basic" proxyCredentialType="None"
         realm="" />
</security>
Run Code Online (Sandbox Code Playgroud)

并在代码中添加:

ws.ClientCredentials.UserName.UserName = "";
ws.ClientCredentials.UserName.Password = "";
Run Code Online (Sandbox Code Playgroud)

现在它似乎可能正在使用凭据,但它给了我错误:

提供的URI方案'http'是无效的URI预期'https'

我甚至都不知道这是不是正确的方法......

ant*_*ioh 34

我在这里发布未来读者的解决方案:

<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>
Run Code Online (Sandbox Code Playgroud)

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

另请注意mode ="TransportCredentialOnly"选项,这允许您使用http而不是使用发送用户名/密码https.这对于我正在使用的测试环境是必要的.后来显然你更愿意https发送你的凭据.

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

var ws = new ***.MyHandlerClient("MyHandler");
ws.ClientCredentials.UserName.UserName = "myUsername";
ws.ClientCredentials.UserName.Password = "myPassword";
var result = ws.executeMyMethod();
Run Code Online (Sandbox Code Playgroud)


blo*_*art 5

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

获得SSL证书后,您可以选择两种方式来选择凭据的发送,传输或安全性以及凭证类型的多个选项.MSDN 对所有各种选项都有很好的指导.