.net – Converting WSE example code to WCF


I am new to both WSE and WCF and I am trying to consume a web service using WCF but all the example documentation is for VS2005 + WSE. This web service uses WS-Security 1.0. I have added a service reference through visual studio but I am at a loss on how to do the equivalent of the code below in WCF:

// 1. Initialize the web service proxy
PartnerAPIWse integrationFramework = new PartnerAPIWse();

// 2. Set the username/password. This is using the Username token of WS-Security 1.0
UsernameTokenProvider utp = new UsernameTokenProvider("username", "password");

// 3. Declare the policy
Policy policy = new Policy(new UsernameOverTransportAssertion());

Best Solution

After spending a day doing some experimentation I figured out how to convert this code. The key was setting up the bindings on the WCF proxy that VS2008 makes correctly.

  1. Add a service reference pointing to the WSDL
  2. Open App.config / Web.config and locate the system.serviceModel section. Change the security mode on the default soapbinding to be TransportWithMessageCredential. Here is what my file looked like after the change:

            <binding name="SoapBinding" closeTimeout="00:01:00" openTimeout="00:01:00"
                receiveTimeout="00:10:00" sendTimeout="00:10:00" allowCookies="false"
                bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <security mode="TransportWithMessageCredential">
                    <transport clientCredentialType="None" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
  3. Change the example code above to look like this

    Dim integrationFramework As New SoapClient()
    integrationFramework.ClientCredentials.UserName.UserName = "username"
    integrationFramework.ClientCredentials.UserName.Password = "password"

TransportWithMessageCredential is equivalent to UsernameOverTransportAssertion policy under WSE 3.0

Related Question