Skip to content

Instantly share code, notes, and snippets.

@aidancasey
Last active December 15, 2015 07:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aidancasey/5222756 to your computer and use it in GitHub Desktop.
Save aidancasey/5222756 to your computer and use it in GitHub Desktop.
Securing and authenticating azure service bus relay messages using a shared secret. : http://acaseyblog.wordpress.com/2013/03/22/securing-and-authenticating-azure-service-bus-relay-messages-using-a-shared-secret/
using (var serviceRequest = new WebClient())
{
string token = GetAuthorizationToken("yourservice","owner", "NW1wr7/nlgggTFnB5L7nDBrOLC+o1E1P4ZZqPaP2mY4=");
serviceRequest.Headers["Authorization"] = string.Format("WRAP access_token=\"{0}\"", token);
string response = serviceRequest.DownloadString(new Uri(url));
return response;
}
// connects to ACS and gets WRAP Authorization token
public string GetAuthorizationToken(string serviceNamespace, string issuerName, string issuerPassword)
{
string acsEndpoint = "https://" + serviceNamespace + "-sb.accesscontrol.windows.net/WRAPv0.9";
string relyingPartyAddress = "http://" + serviceNamespace + ".servicebus.windows.net";
NameValueCollection postData = new NameValueCollection
{
{ "wrap_scope", relyingPartyAddress },
{ "wrap_name", issuerName },
{ "wrap_password", issuerPassword },
};
WebClient webClient = new WebClient();
byte[] responseBuffer = webClient.UploadValues(acsEndpoint, "POST", postData);
string response = Encoding.UTF8.GetString(responseBuffer);
string encodedtoken = response.Split('&')
.Single(value => value.StartsWith("wrap_access_token="))
.Split('=')[1];
string token = System.Web.HttpUtility.UrlDecode(encodedtoken);
return token;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment