Instantly share code, notes, and snippets.

Embed
What would you like to do?
<policies>
<inbound>
<set-backend-service base-url="*** add your wcf relay address here ***" />
<cache-lookup-value key="@("relaytoken")" variable-name="relaytoken" />
<choose>
<when condition="@(!context.Variables.ContainsKey("relaytoken"))">
<set-variable name="resourceUri" value="@(context.Request.Url.ToString())" />
<!-- an accessKey entry must exist in NameValue -->
<set-variable name="accessKey" value="{{accessKey}}" />
<!-- an keyName entry must exist in NameValue -->
<set-variable name="keyName" value="{{accessKeyName}}" />
<set-variable name="relaytoken" value="@{
TimeSpan sinceEpoch = DateTime.UtcNow - new DateTime(1970, 1, 1);
string expiry = Convert.ToString((int)sinceEpoch.TotalSeconds + 3600);
string resourceUri = (string)context.Variables["resourceUri"];
string stringToSign = Uri.EscapeDataString (resourceUri) + "\n" + expiry;
HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes((string)context.Variables["accessKey"]));
string signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
string sasToken = String.Format("SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}",
Uri.EscapeDataString(resourceUri), Uri.EscapeDataString(signature), expiry, context.Variables["keyName"]);
return sasToken;
}" />
<cache-store-value key="relaytoken" value="@((string)context.Variables["relaytoken"])" duration="10" />
</when>
</choose>
<set-body template="liquid">
<!-- set your body transformation here -->
</set-body>
<set-header name="ServiceBusAuthorization" exists-action="override">
<value>@((string)context.Variables["relaytoken"])</value>
</set-header>
<set-header name="Content-Type" exists-action="override">
<value>application/xml</value>
</set-header>
<base />
</inbound>
<backend>
<base />
</backend>
<outbound>
<set-body template="liquid">
<!-- set your body transformation here -->
</set-body>
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
</outbound>
<on-error>
<base />
</on-error>
</policies>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment