Skip to content

Instantly share code, notes, and snippets.

@satish860
Created October 11, 2012 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satish860/3872727 to your computer and use it in GitHub Desktop.
Save satish860/3872727 to your computer and use it in GitHub Desktop.
Handler
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