I must be doing something wrong because I have replicated many answers on this subject. My attribute binding is not being hit and I'm not sure why.
Controller.cs
[NatGeoUserAccessAuthorization]
[HttpGet]
public virtual ActionResult Teacher(string id)
{
Attribute/Fitler
public class NatGeoUserAccessAuthorizationAttribute : FilterAttribute{}
public class NatGeoUserAccessAuthorizationFilter : IAuthorizationFilter
{
private readonly IUsersService _usersService;
public NatGeoUserAccessAuthorizationFilter(IUsersService usersService)
{
_usersService = usersService;
}
public string QueryStringName { get; set; }
#region Implementation of IAuthorizationFilter
public void OnAuthorization(AuthorizationContext filterContext)
{
if (!_usersService.HasUserAccess(filterContext.HttpContext.User.Identity.Name, filterContext.HttpContext.Request.QueryString[QueryStringName ?? "id"]))
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary { { "action", "AccessDenied" }, { "controller", "Error" } });
}
}
#endregion
}
Global.asax
protected void Application_Start()
{
// NLog Custom Layouts
ConfigurationItemFactory.Default.LayoutRenderers.RegisterDefinition("utc_date", typeof(UtcDateRenderer));
ConfigurationItemFactory.Default.LayoutRenderers.RegisterDefinition("web_variables", typeof(WebVariablesRenderer));
// Setup IoC Container
DependencyResolver.SetResolver(Bootstrap.Configure((kernel) =>
{
kernel.Bind<IDatabaseFactory>().To<DatabaseFactory<MySqlConnection>>().InRequestScope().WithConstructorArgument("connectionString", Config.Data.MySQLConnection);
ManagerBindings.Register(kernel);
ProviderBindings.Register(kernel);
RepositoryBindings.Register(kernel);
ServiceBindings.Register(kernel);
ValidationBindings.Register(kernel);
kernel.BindFilter<NatGeoUserAccessAuthorizationFilter>(FilterScope.Action, 0).WhenActionMethodHas<NatGeoUserAccessAuthorizationAttribute>();
kernel.Bind<IUserProfile>().To<UserProfile>();
kernel.Inject(Roles.Provider);
}));
// Custom Default Model Binder
ModelBinders.Binders.DefaultBinder = new ValidationModelBinder();
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
Bootstrap.cs
public class Bootstrap
{
public delegate void IocRegistrationDelegate(IKernel kernel);
public static IDependencyResolver Configure(IocRegistrationDelegate serviceBindings)
{
var kernel = new StandardKernel();
serviceBindings.Invoke(kernel);
return new NinjectDependencyResolver(kernel);
}
}
You are using an own bootstrapping mechanism that doesn't support filter bindings. Read the docu how to use the one that comes with Ninject.Mvc3 on http://github.com/ninject/ninject.web.mvc/wiki