使用 Ninject 和全局过滤器进行依赖注入:IAuthorizationFilter

发布于 2025-01-01 00:12:04 字数 1229 浏览 1 评论 0原文

我使用安装在 App_Start 文件夹中的标准 NinjectMVC3 Bootstrapper。

我的应用程序类如下所示:

public class MvcApplication : HttpApplication
{
    static void RegisterRoutes(RouteCollection routes)
    {
        // ... routes here ...
    }

    public void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }

    static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        // empty
    }
}

我在 NinjectMVC3 中仅注册了以下出价规则:

Bind<IAccountsRepository>().To<AccountsRepository>();
this.BindFilter<GlobalAuthFilter>(FilterScope.Global, 0);

我的全局过滤器:

public class GlobalAuthFilter : IAuthorizationFilter
{
    readonly IAccountsRepository _accountsRepository;

    public GlobalAuthFilter(IAccountsRepository accountsRepository)
    {
        _accountsRepository = accountsRepository;
    }

    public void OnAuthorization(AuthorizationContext context)
    {
        // Code here never reached. Why? What's wrong?
    }
}

我的应用程序中有任何控制器。我想为每个控制器的每个操作调用调用 OnAuthorization。

但我的代码不起作用。谢谢。

I use standart NinjectMVC3 Bootstrapper installed in the App_Start folder.

My application class looks like:

public class MvcApplication : HttpApplication
{
    static void RegisterRoutes(RouteCollection routes)
    {
        // ... routes here ...
    }

    public void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }

    static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        // empty
    }
}

I have only following bidding rules registed in NinjectMVC3:

Bind<IAccountsRepository>().To<AccountsRepository>();
this.BindFilter<GlobalAuthFilter>(FilterScope.Global, 0);

And my global filter:

public class GlobalAuthFilter : IAuthorizationFilter
{
    readonly IAccountsRepository _accountsRepository;

    public GlobalAuthFilter(IAccountsRepository accountsRepository)
    {
        _accountsRepository = accountsRepository;
    }

    public void OnAuthorization(AuthorizationContext context)
    {
        // Code here never reached. Why? What's wrong?
    }
}

There are any controllers in my application. And I want to invoke OnAuthorization for every action calls for every controllers.

But my code dosn't work. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

远昼 2025-01-08 00:12:04

从您的代码中并不清楚您在哪里更具体地配置内核。这应该在 ~/App_Start/NinjectMVC3.csRegisterServices 方法中完成:

/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
    kernel.Bind<IAccountsRepository>().To<AccountsRepository>();
    kernel.BindFilter<GlobalAuthFilter>(FilterScope.Global, 0);
}        

当您安装 Ninject.MVC3 NuGet 包的主体时此方法将为空,您应该直接配置依赖项或定义要在此方法中导入的 Ninject 模块:

/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
    kernel.Load(new MyModule());
}        

您定义了自定义模块的位置:

public class MyModule : NinjectModule
{
    public override void Load()
    {
        this.Bind<IAccountsRepository>().To<AccountsRepository>();
        this.BindFilter<GlobalAuthFilter>(FilterScope.Global, 0);
    }
}

It's not quite clear from your code where more specifically are you configuring your kernel. This should be done in the RegisterServices method of ~/App_Start/NinjectMVC3.cs:

/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
    kernel.Bind<IAccountsRepository>().To<AccountsRepository>();
    kernel.BindFilter<GlobalAuthFilter>(FilterScope.Global, 0);
}        

When you install the Ninject.MVC3 NuGet package the body of this method will be empty and it is where you should either directly configure dependencies or define Ninject modules that you would import in this method:

/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
    kernel.Load(new MyModule());
}        

where you have defined the custom module:

public class MyModule : NinjectModule
{
    public override void Load()
    {
        this.Bind<IAccountsRepository>().To<AccountsRepository>();
        this.BindFilter<GlobalAuthFilter>(FilterScope.Global, 0);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文