Best way to abort/cancel action and response from ActionFilter

Setting the response will mean the action doesn’t get called. public override void OnActionExecuting(HttpActionContext actionContext) { actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized); } As other answers have said, though, authentication should be done with an AuthorizeAttribute (Docs for Web.API or for MVC).

Get User Name on Action Filter

Bit late for an answer but this is best solution if you are using HttpActionContext in your filter You can always use it as mentioned here:- public override Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken) { if (actionContext.RequestContext.Principal.Identity.IsAuthenticated) { var userName = actionContext.RequestContext.Principal.Identity.Name; } }

How to pass parameters to a custom ActionFilter in ASP.NET MVC 2?

This is a way to make this work. You have access to the ControllerContext and therefore Controller from the ActionFilter object. All you need to do is cast your controller to the type and you can access any public members. Given this controller: public GenesisController : Controller { [CheckLoggedIn()] public ActionResult Home(MemberData md) { return … Read more

Best place to set CurrentCulture for multilingual ASP.NET MVC web applications

I used a global ActionFilter for this, but recently I realized, that setting the current culture in the OnActionExecuting method is too late in some cases. For example, when model after POST request comes to the controller, ASP.NET MVC creates a metadata for model. It occurs before any actions get executed. As a result, DisplayName … Read more

Why call base.OnActionExecuting(filterContext);?

Should I be always be calling the base methods after? That will depend on the situation. For example in authorization filters (deriving from AuthorizeAttribute) if you call the base method then all the existing authorization logic built into ASP.NET MVC will be executed. If you don’t call it, only your authorization logic will be applied. … Read more

Redirecting to specified controller and action in asp.net mvc action filter

Rather than getting a reference to HttpContent and redirecting directly in the ActionFilter you can set the Result of the filter context to be a RedirectToRouteResult. It’s a bit cleaner and better for testing. Like this: public override void OnActionExecuting(ActionExecutingContext filterContext) { if(something) { filterContext.Result = new RedirectToRouteResult( new RouteValueDictionary {{ “Controller”, “YourController” }, { … Read more

Async OnActionExecuting in ASP.NET Core’s ActionFilterAttribute

Asynchronous filters work a bit differently: first execute code that must be executed before the action, call next() for the actual logic, finally add code to be executed after the action. public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { // logic before action goes here await next(); // the actual action // logic after the … Read more