MVC Hangfire授权Ninject

时间:2015-09-28 14:55:52

标签: asp.net-mvc ninject hangfire

我想向Hangfire AuthorizationFilter注入我的自定义服务并用它做一些我面临的问题我的服务总是为空。

Owin启动课程:

/work/app.py

自定义过滤器:

 public class Startup
{

    public void Configuration(IAppBuilder app )
    {
        GlobalConfiguration.Configuration
            .UseSqlServerStorage("SomeContext")
            .UseNinjectActivator(new Bootstrapper().Kernel);

        app.UseHangfireDashboard("/hangfire", new DashboardOptions { AuthorizationFilters = new[] { new RestrictiveAuthorizationFilter() } });
        app.UseHangfireServer();
    }
}

Ninject内核:

public class RestrictiveAuthorizationFilter : IAuthorizationFilter
{

    [Inject]
    public IUserService _userService { get; set; }

    public bool Authorize(IDictionary<string, object> owinEnvironment)
    {

      //do something with _userService but it is always null

      return true;
    }
}

我没有看到我的错误你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

You can pass the kernel as an argument to RestrictiveAuthorizationFilter constructor and ask for the IUserService.

public class RestrictiveAuthorizationFilter : IAuthorizationFilter 
{   
    public IUserService _userService { get; set; }

    public RestrictiveAuthorizationFilter(IKernel kernel)
    {
        _userService = kernel.Get<IUserService>()
    }

    public bool Authorize(IDictionary<string, object> owinEnvironment)
    {

      //do something with _userService but it is always null

      return true;
    }
}