Skip to content

Instantly share code, notes, and snippets.

@srkirkland
Created May 25, 2017 17:29
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 srkirkland/f72be06b457d1dad8dae87e12811d70e to your computer and use it in GitHub Desktop.
Save srkirkland/f72be06b457d1dad8dae87e12811d70e to your computer and use it in GitHub Desktop.
Token filter for securing APIs
using System;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Configuration;
using System.Web.Http;
using System.Web.Http.Controllers;
namespace GivingService.Attributes
{
public class AuthTokenFilter : System.Web.Http.Filters.AuthorizationFilterAttribute
{
private static readonly string ValidToken = WebConfigurationManager.AppSettings["token"];
/// <summary>
/// Look in the X-Auth-Token header and also the params for a token, and then check it against the proper value
/// </summary>
/// <param name="actionContext"></param>
public override void OnAuthorization(HttpActionContext actionContext)
{
//First check the preferred token header
var tokenHeader =
actionContext.Request.Headers.SingleOrDefault(
h => string.Equals(h.Key, "X-Auth-Token", StringComparison.InvariantCultureIgnoreCase));
if (tokenHeader.Value != null &&
string.Equals(tokenHeader.Value.First(), ValidToken, StringComparison.InvariantCultureIgnoreCase))
{
return;
}
//Else try the query string token
var query = HttpUtility.ParseQueryString(actionContext.Request.RequestUri.Query);
if (string.Equals(query["token"], ValidToken, StringComparison.InvariantCultureIgnoreCase))
{
return;
}
//Nothing is valid, so reject
throw new HttpResponseException(HttpStatusCode.Unauthorized);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment