Wednesday 13 January 2016

How to programmatically add Basic Authorization Headers to a message in BizTalk

In order to apply a basic authorization header to a BizTalk transmission over HTTPS you first need to create a c# function in order to construct the header and provide with the relevant username and password

First thing to do is to create a c# project in your BizTalk Solution, and within that project (mines called Test), create a module class called Encryptor.  Within that, create the function BasicAuth as shown in the code below (for you code purists who've noticed .... yes I haven't put a try, catch block in there .... I'll leave that to you :) )

namespace Test
    public class Encryptor
     public static string BasicAuth(string strUser, string strPass)
string strBasicAuth = ""; 
            if (strUser != "" && strPass != "")
                 byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(strUser + ":" + strPass);
                string strBasicAuthConv = System.Convert.ToBase64String(toEncodeAsBytes);
                strBasicAuth = "Authorization: Basic " + strBasicAuthConv;
              return strBasicAuth;

So in the function depicted above we create a byte array containing the Username and Password separated by a colon.  Then convert that array to a base64 string and finally prefix that string with “Authorization: Basic “ which will produce a string something like …. Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=

Now in your project which contains the orchestration that will send the message add a reference to the c# project so that you can use the function that you have written 

Next in your orchestration, find your constructed message ….. add a message assignment shape to the message construction block (at the bottom, not necessary but let’s not get fussy about shape position) and add the following code, in the picture below

strUsername and strPassword being the username and password you want to send in your message, CustomerInvoiceOutMsg being the name of the constructed message

Build your projects / deploy and test … job done J

No comments:

Post a Comment