Check AllowAnonymousAttribute in your custom filter. Here is one possible way to do this.
Add the following extension method.
public static class MyExtensionMethods { public static bool HasAttribute(this ActionExecutingContext context, Type attribute) { var actionDesc = context.ActionDescriptor; var controllerDesc = actionDesc.ControllerDescriptor; bool allowAnon = actionDesc.IsDefined(attribute, true) || controllerDesc.IsDefined(attribute, true); return allowAnon; } }
Then use it in your filter.
public class MyActionFilter : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { // use the extension method in your filter if (filterContext.HasAttribute(typeof(AllowAnonymousAttribute))) { // exit early... return; } // ...or do whatever else you need to do if (user == null || !user.Active) { filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary { { "controller", "Home" }, { "action", "NotAuthorized" } }); } base.OnActionExecuting(filterContext); } }
Here is a script that implements the solution .
source share