Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use Ninject to inject services into an authorization filter?
    primarykey
    data
    text
    <p>I am using asp.net mvc 3, ninject 2.0 and the ninject mvc 3 plugin.</p> <p>I am wondering how do I get service layers into my filter(in this case an authorization filter?).</p> <p>I like to do constructor inject so is this possible or do I have to property inject?</p> <p>Thanks</p> <p>Edit</p> <p>I have this for property inject but my property is always null</p> <pre><code> [Inject] public IAccountService AccountServiceHelper { get; set; } protected override bool AuthorizeCore(HttpContextBase httpContext) { // check if context is set if (httpContext == null) { throw new ArgumentNullException("httpContext"); } // check if user is authenticated if (httpContext.User.Identity.IsAuthenticated == true) { // stuff here return true; } return false; } /// &lt;summary&gt; /// Application_Start /// &lt;/summary&gt; protected void Application_Start() { // Hook our DI stuff when application starts IKernel kernel = SetupDependencyInjection(); RegisterMaps.Register(); AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); } public IKernel SetupDependencyInjection() { IKernel kernel = CreateKernel(); // Tell ASP.NET MVC 3 to use our Ninject DI Container DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel)); return kernel; } protected IKernel CreateKernel() { var modules = new INinjectModule[] { new NhibernateModule(), new ServiceModule(), new RepoModule() }; return new StandardKernel(modules); } public class ServiceModule : NinjectModule { public override void Load() { Bind&lt;IAccountService&gt;().To&lt;AccountService&gt;(); } } </code></pre> <p><strong>Edit</strong></p> <p>I upgraded to ninject 2.2 and get finally got it work.</p> <p><strong>Edit 2</strong></p> <p>I am going to try and do the constructor way for my authorize filter but I am unsure how to pass in the Roles. I am guessing I have to do it through ninject? </p> <p><strong>Edit 3</strong></p> <p>This is what I have so far</p> <pre><code> public class MyAuthorizeAttribute : AuthorizeAttribute { private readonly IAccountService accountService; public MyAuthorizeAttribute(IAccountService accountService) { this.accountService = accountService; } protected override bool AuthorizeCore(HttpContextBase httpContext) { return base.AuthorizeCore(httpContext); } } this.BindFilter&lt;MyAuthorizeAttribute&gt;(FilterScope.Controller, 0) .WhenControllerHas&lt;MyAuthorizeAttribute&gt;(); [MyAuthorize] public class MyController : BaseController { } </code></pre> <p>It tells me it want's a no parameter constructor. So I must be missing something.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload