Thursday, April 16, 2009

Sending credentials from InvokeWebService activity.

Using Windows Workflow Foundation (WF) to make a call to a web service is pretty straightforward. In most cases, you just drop on the InvokeWebService activity and do some basic configuration.

However, if you are invoking a web service that requires HTTP based authentication before it can be used, you'll need to attach credentials to the outbound request. It took me a while to sort this out, but found its actually pretty easy. You'll know if you need to send credentials because you'll get a HTTP 401: Unauthorized message back if you don't provide them.

The trick is to leverage the InvokeWebServer.Invoking event. Here are the steps:

1. Right click on your invokeWebService activity and click 'Generate Handlers'. This will add two handlers to your code file:


Private Sub invokeWebServiceActivity1_Invoked(ByVal sender As System.Object, ByVal e As System.Workflow.Activities.InvokeWebServiceEventArgs)

End Sub

Private Sub invokeWebServiceActivity1_Invoking(ByVal sender As System.Object, ByVal e As System.Workflow.Activities.InvokeWebServiceEventArgs)

End Sub

2. In the Invoking handler, set your credentials to the WebServiceProxy class.


Private Sub invokeWebServiceActivity1_Invoking(ByVal sender As System.Object, ByVal e As System.Workflow.Activities.InvokeWebServiceEventArgs)
e.WebServiceProxy.Credentials = New System.Net.NetworkCredential("username", "password", "domain")

End Sub


You can also use the NetworkCredential object to impersonate the current user, which would be the user that the Workflow Host is running as.

No comments:

Post a Comment