Created
October 24, 2018 11:42
-
-
Save stalko/beafdc843840061e2865138127efe2fa to your computer and use it in GitHub Desktop.
Example action filter attribute for ASP.NET WEB API
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
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