Search

Locations of visitors to this page

Categories

On this page

Setting UserName ClientCredentials in WF SendActivity

Archive

Blogroll

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 41
This Year: 16
This Month: 0
This Week: 0
Comments: 54

Sign In

 Monday, June 02, 2008
Monday, June 02, 2008 10:35:06 AM (GMT Standard Time, UTC+00:00) ( WF )

I was recently asked if there is a way to set custom UserName credentials in a SendActivity. I did some investigation and couldn’t find any OOB way do achieve this. So the next step was to look into WCF’s awesome extensibility mechanism **Bahaviors**. Turns out you can achieve this functionality quite easily by writing a custom endpoint behavior.

Step 1 would be to create a custom endpoint behavior:

public class UserNameBehavior : IEndpointBehavior

{

    #region IEndpointBehavior Members

    public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)

    {}

    public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)

    {

        var cc = endpoint.Behaviors.Find<ClientCredentials>();

        cc.UserName.UserName = "zahmed";

        cc.UserName.Password = "password";

    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)

    {}

    public void Validate(ServiceEndpoint endpoint)

    {}

    #endregion

}

In the ApplyClientBehavior method, find the appropriate behavior object and override its properties to your desired values. Now you have to specify this behavior in the endpoint configuration of the SendActivity.

To use your custom behavior from config you have to create a BehaviorElementExtension(Step 2)

    public class UserNameBehaviorElement : BehaviorExtensionElement

    {

        protected override object CreateBehavior()

        {

            return new UserNameBehavior();

        }

        public override Type BehaviorType

        {

            get { return typeof(UserNameBehavior); }

        }

    }

Step 3 would be to register your behavior as an extension in the config file:

<extensions>

  <behaviorExtensions>

    <add name="userName" type="WorkflowConsoleApplication1.UserNameBehaviorElement, WorkflowConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>

  </behaviorExtensions>

</extensions>

At this point your custom behavior is ready to be used in the endpoint configuration used by the SendActivity. Here is the complete config file.

<configuration>

  <system.serviceModel>

    <bindings>

      <basicHttpBinding>

        <binding name="BasicHttpBinding ICalcultor">

          <security mode="Transport">

            <transport clientCredentialType="Basic" />

          </security>

        </binding>

      </basicHttpBinding>

    </bindings>

    <client>

      <endpoint address="https://localhost:7000/Calc" binding="basicHttpBinding"

          bindingConfiguration="BasicHttpBinding ICalcultor" contract="ServiceReference1.ICalcultor"

          name="BasicHttpBinding ICalcultor" behaviorConfiguration="userNameBehavior" />

    </client>

    <behaviors>

      <endpointBehaviors>

        <behavior name="userNameBehavior">

          <userName/>

        </behavior>

      </endpointBehaviors>

    </behaviors>

    <extensions>

      <behaviorExtensions>

        <add name="userName" type="WorkflowConsoleApplication1.UserNameBehaviorElement, WorkflowConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>

      </behaviorExtensions>

    </extensions>

  </system.serviceModel>

</configuration>