Skip to content

Instantly share code, notes, and snippets.

@stalko
Created October 24, 2018 11:42
Show Gist options
  • Save stalko/beafdc843840061e2865138127efe2fa to your computer and use it in GitHub Desktop.
Save stalko/beafdc843840061e2865138127efe2fa to your computer and use it in GitHub Desktop.
Example action filter attribute for ASP.NET WEB API
using System.Linq;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
namespace Server.Attributes
{
public class ExampleFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (!actionContext.Request.Headers.Contains("Authorization"))
throw new Exception("Token not found");
else
{
var token = actionContext.Request.Headers.GetValues("Authorization").FirstOrDefault();
User(token).Check();//Throw error if have a problem with this token
}
//Go to the next step
base.OnActionExecuting(actionContext);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment