Skip to content

Instantly share code, notes, and snippets.

@jpda
Created January 12, 2015 21:27
Show Gist options
  • Save jpda/93daee736e3dcdbb866f to your computer and use it in GitHub Desktop.
Save jpda/93daee736e3dcdbb866f to your computer and use it in GitHub Desktop.
AuthorizationHeaderMessageInspector.cs
public class AuthorizationHeaderMessageInspector : IClientMessageInspector
{
private readonly string _token;
public AuthorizationHeaderMessageInspector(string token)
{
_token = string.IsNullOrEmpty(token) ? AzureAdToken.Get() : token;
}
public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
{
HttpRequestMessageProperty httpRequest;
if (request.Properties.ContainsKey(HttpRequestMessageProperty.Name))
{
httpRequest = request.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
}
else
{
httpRequest = new HttpRequestMessageProperty();
}
if (httpRequest != null) httpRequest.Headers.Add("Authorization", string.Format("Bearer {0}", _token));
return null;
}
public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment