Created
August 29, 2014 21:40
-
-
Save darrelmiller/ee4c74f34fa23ef7d5df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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