Skip to content

Instantly share code, notes, and snippets.

@darrelmiller
Created August 29, 2014 21:40
Show Gist options
  • Save darrelmiller/ee4c74f34fa23ef7d5df to your computer and use it in GitHub Desktop.
Save darrelmiller/ee4c74f34fa23ef7d5df to your computer and use it in GitHub Desktop.
public class TokenValidationHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
const string errorMessage = "Unauthorized access";
const HttpStatusCode code = HttpStatusCode.Unauthorized;
var authValue = request.Headers.Authorization;
if (authValue == null)
return CreateErrorResponse(errorMessage, code);
if (String.IsNullOrWhiteSpace(authValue.Parameter))
return CreateErrorResponse(errorMessage, code);
if (string.CompareOrdinal(authValue.Parameter, 0, authValue.Parameter, 0, authValue.Parameter.Length) != 0)
return CreateErrorResponse(errorMessage, code);
return await base.SendAsync(request, cancellationToken);
}
private static HttpResponseMessage CreateErrorResponse(string errorMessage, HttpStatusCode httpStatusCode)
{
return new HttpResponseMessage(httpStatusCode)
{
Content = new StringContent(errorMessage)
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment