Created
October 11, 2012 14:26
-
-
Save satish860/3872727 to your computer and use it in GitHub Desktop.
Handler
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Web; | |
namespace WebAPIKoans.MessageHandlers | |
{ | |
public class BasicAuthenticationHandler : DelegatingHandler | |
{ | |
Task<HttpResponseMessage> UnauthorizedTask = Task.Factory.StartNew(() => | |
{ | |
// Returns the Unauthorized Message (401) which actually challenges the client to issue authentication | |
return new HttpResponseMessage(HttpStatusCode.Unauthorized); | |
}); | |
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | |
{ | |
//If the authorization Header is not found then challenge for the token | |
if (request.Headers.Authorization == null) | |
{ | |
return UnauthorizedTask; | |
} | |
//If the Scheme and the authroization is correct then send it to the next handler | |
if (request.Headers.Authorization.Scheme == "Basic" && !string.IsNullOrEmpty(request.Headers.Authorization.Scheme)) | |
return base.SendAsync(request, cancellationToken); | |
return UnauthorizedTask; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment