1

Authorization filter attribute is not firing

 public class AuthenticationRequiredAttribute : System.Web.Http.Filters.AuthorizationFilterAttribute
    {
        public override bool AllowMultiple
        {
            get { return false; }
        }
        
        public override void OnAuthorization(HttpActionContext actionContext)
        {            
            base.OnAuthorization(actionContext);
           
        }

I have decorated controller with the attribute

 [AuthenticationRequired]
    public class ProfileController : BaseController
    {        
        public ProfileController(IMyRepository repository)
            : base(repository)
        {
        }
    }

the AuthenticationRequired filter is not firing

3 Answers 3

0

I believe you need to add an Initialize pipeline processor to add the new filter.

<initialize>
  <processor type="InitializeNamespace.InitializeClassName, LibraryName" patch:after="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeGlobalFilters, Sitecore.Mvc']" resolve="true">
  </processor>
</initialize>
using System.Collections.Generic;
using System.Web.Http;
using Sitecore.Pipelines;

namespace InitializeNamespace
{
    public class InitializeClassName
    {       
        public virtual void Process(PipelineArgs args)
        {
            GlobalConfiguration.Configuration.Filters.Add(new AuthenticationRequiredAttribute());
        }
    }
}
1
  • I tried this but it does not work. code is still not getting executed Commented Oct 23, 2023 at 5:10
0

Try inheriting your class from FilterAttribute, IAuthorizationFilter.

using System.Web.Mvc;
public class AuthenticationRequiredAttribute : FilterAttribute, IAuthorizationFilter
{
    public void OnAuthorization(AuthorizationContext filterContext)
    {
        //your custom logic goes here.
    }
}

Decorate your controller with attributes.

[AuthenticationRequired]
public class ProfileController : Controller
{        
   public ProfileController(IMyRepository repository) : base(repository)
   {
   }
}

Hope it helps!

0

You seem to be using a Web API filter instead of an MVC filter. Inherit your custom authorization filter from System.Web.Mvc.FilterAttribute class and System.Web.Mvc.IAuthorizationFilter interface, and implement OnAuthorization() method.

For example,

using System.Web.Mvc;
using Sitecore;


namespace {YOUR_PROJECT_NAMESPACE}
{
    public class AuthenticationRequired : System.Web.Mvc.FilterAttribute, System.Web.Mvc.IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            {YOUR_CUSTOM_CODE_LOGIC}
        }
    }

}

You can configure your custom filter in your application at three levels:

  • Global level by registering your filter in Application_Start event of Global.asax.cs;

  • Controller level by decorating a controller with your filter by putting your filter at the top of the controller name;

  • Action level by decorating a given action method with your filter in a similar way as above for controllers.

In your case you simply need to decorate your controller with your custom filter.

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.