Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7544879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:47:15+00:00 2026-05-30T08:47:15+00:00

In a project I’m working on there are two custom ActionFilterAttribute classes which are

  • 0

In a project I’m working on there are two custom ActionFilterAttribute classes which are injected using ninjects BindFilter:

        kernel.BindFilter<LogErrorsAttribute>(FilterScope.Last, 0);
        kernel.BindFilter<CriticalErrorAttribute>(FilterScope.Last, 1);

These have been working fine.

I created a custom IAuthorizationFilter filter which is also injected using BindFilter:

        kernel.BindFilter<AuthorizationFilter>(FilterScope.Action, null).WhenActionMethodHas<Authorise>().WithPropertyValueFromActionAttribute<Authorise>("Roles", n => n.Roles).WithPropertyValueFromActionAttribute<Authorise>("Years", n => n.Years);

On it’s own, this works fine too.

I just discovered that if I apply the Authorise tag to an action, the two custom ActionFilterAttribute classes are no longer called.

I’m stumped on why this would be occuring. My custom IAuthorizationFilter looks like this:

public class AuthorizationFilter : IAuthorizationFilter
{
    private readonly string[] RolesHaveAccessToApplication;

    public AuthorizationFilter()
    {
        //put roles which should allow user to see application, hardcoded for now, but later
        //this can be generated from the database
        var configRoles = ConfigurationManager.AppSettings["ApplicationRoles"];

        if(string.IsNullOrEmpty(configRoles))
            throw new Exception("The ApplicationRoles value has not been defined in the web.config file.");

        RolesHaveAccessToApplication = configRoles.Split(',');

    }

    [Inject]
    public IUserServices userService { get; set; }

    public string Roles { get; set; }
    public string Years { get; set; }


    protected bool AuthorizeCore(HttpContextBase httpContext)
    {
        if (!httpContext.Request.IsAuthenticated)
            return false;


        if(!Roles.HasContent() && !Years.HasContent())
        {
            return RolesHaveAccessToApplication.Any(role => RolesHaveAccessToApplication.Any(n => n == role));

        }

        var AuthenticatedUserRoles = System.Web.Security.Roles.GetRolesForUser();
        bool isAuthorised = false;

        //first, lets check against to see if the user has any roles related to the application
        isAuthorised = RolesHaveAccessToApplication.Any(role => AuthenticatedUserRoles.Any(n => n == role));

        //if they don't, we throw them to access denied page
        if (!isAuthorised)
            return false;

        #region CheckRoles
        if (!string.IsNullOrEmpty(Roles) && AuthenticatedUserRoles.HasContent())
        {
            var authRoles = Roles.Split(new [] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            isAuthorised = authRoles.Any(role => AuthenticatedUserRoles.Any(n => n == role));
        }
        #endregion

        #region CheckYears
        if (!string.IsNullOrEmpty(Years) && AuthenticatedUserRoles.HasContent())
        {


            if (AuthenticatedUserRoles.Any(n => n == "Student"))
            {
                var yearRoles = Years.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                var user = userService.FetchUser(httpContext.User.Identity.Name);
                if (user != null)
                {
                    isAuthorised = yearRoles.Any(n => n == user.Year);
                }
            }

        }
        #endregion

        return isAuthorised;
    }

    public void OnAuthorization(AuthorizationContext filterContext)
    {
        if(filterContext == null)
            throw new Exception("filtercontext is null");

        if (!filterContext.HttpContext.Request.IsAuthenticated)
            HandleUnauthorizedRequest(filterContext);

            if (AuthorizeCore(filterContext.HttpContext))
                SetCachePolicy(filterContext);
            else
                HandleUnauthorizedRequest(filterContext);


    }

    protected void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        if (filterContext.HttpContext.Request.IsAuthenticated)
            filterContext.Result = new RedirectResult(new UrlHelper(filterContext.RequestContext).Action("Denied", "Home", new {Area = ""}));// new ViewResult { ViewName = "~/Home/Denied", View = new RazorView("Home") //ViewData = viewData }; 
        else
            filterContext.Result = new HttpUnauthorizedResult();

    }

    protected void SetCachePolicy(AuthorizationContext filterContext)
    {
           ..snip..
    } 

}

//Used as a filter for actions, and ninject is configured to bind AuthorizationFilter to this
public class Authorise : ActionFilterAttribute
{
    public string Roles { get; set; }
    public string Years { get; set; }
}

Any help with resolving this would be appreciated.

Edit:

This is one of the other filters:

public class CriticalErrorAttribute : ActionFilterAttribute
{
    [Inject]
    public IErrorServices ErrorService { private get; set; }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {            //if the request is an ajax request, we don't want a redirect to happen
        //the controller dealing with the ajax request can fetch the critical
        //errors and pass them back to the user for display
        if (!filterContext.HttpContext.Request.IsAjaxRequest())
        {
            var criticalErrors = ErrorService.FetchCriticalErrors();

            if (criticalErrors.HasContent())
            {
                var helper = new UrlHelper(filterContext.RequestContext);
                var url = helper.Action("Error", "Home", new { area = "" });

                filterContext.Controller.TempData["CriticalErrorList"] = criticalErrors;

                filterContext.Result = new RedirectResult(url);
            }
        }
        base.OnActionExecuted(filterContext);
    }
}

If a domain object couldnt be hydrated, it would log a critical error. This filter checks for such errors, if they occur it directs the user to an error page.

Solved:

It turns out Darin was correct! But the issue was being hidden by my configuration of my filters.
Firstly, I had [Authorise] on navigation items, and secondly I was binding the CriticalErrorAttribute to every action.
So each time a menu was built (welcome, left, nav, sub) – this filter was firing. At some point during this chain of filter calls, results were being applied to filterContext.Result – the later results hiding the earlier (correct) result.

To overcome this issue, I tweaked the BindFilter configuration line for CriticalErrorAttribute to this:

kernel.BindFilter<CriticalErrorAttribute>(FilterScope.Last, 0).When( (context, ad) =>
context.RouteData.DataTokens["action"] != null && context.RouteData.DataTokens["action"] !=
"Error" && context.RouteData.DataTokens["controller"] != "Navigation");

Everything works perfectly now!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:47:16+00:00Added an answer on May 30, 2026 at 8:47 am

    Here:

    filterContext.Result = ...
    

    you are assigning a result. And according to the documentation:

    You can cancel filter execution in the OnActionExecuting and
    OnResultExecuting methods by setting the Result property to a non-null
    value. Any pending OnActionExecuted and OnActionExecuting filters will
    not be invoked and the invoker will not call the OnActionExecuted
    method for the canceled filter or for pending filters. The
    OnActionExecuted filter for previously run filters will run. All of
    the OnResultExecutingand OnResultExecuted filters will run.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My project is currently using a svn repository which gains several hundred new revisions
Our project uses XJC to generate Java classes from an XSD. I'm using JAVA
My Project have two Screen in which Activity A and Activity B. Activity A
A project I'm working on at the moment involves refactoring a C# Com Object
Project I'm working on uses jQuery. I have a series of Ajax calls being
project is written on php. There is timestamp field in mysql it updates automatically.
Project file here if you want to download: http://files.me.com/knyck2/918odc So I am working on
Our project is using many static libraries to build the application. How can we
The project I am currently working on has, in the checkout, an option to
The project I'm working on has an Oracle backend, and we use SQL Navigator

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.