ASP.NET MVC3 IAuthorisationfilter Ninject依赖

时间:2011-04-12 12:27:40

标签: c# asp.net asp.net-mvc-3 ninject

目前我正在尝试设置依赖注入并覆盖授权属性。我试图遵循一些例子,虽然我似乎总是出错。

Global.asax中

public class MvcApplication : Ninject.Web.Mvc.NinjectHttpApplication
{
    private class MyModule : NinjectModule
    {
        public override void Load()
        {
            this.BindFilter<SageAdminAuthorizationFilter>(FilterScope.Controller, 0);
            Bind<IAuthentication>().To<CustomAuthenticationService>();
        }
    }

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

    protected override void OnApplicationStarted()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }

    protected override IKernel CreateKernel()
    {
        var modules = new INinjectModule[] {
        new MyModule()
    };
        var kernel = new StandardKernel(modules);

        return kernel;
    }
}

身份验证过滤器

public class CustomAuthenticationService : IAuthentication
{
    public void SignIn(string claimedIdentifier, bool createPersistentCookie)
    {
        //Write Sign in code here
        FormsAuthentication.SetAuthCookie(claimedIdentifier, createPersistentCookie);
    }

    public void SignOut()
    {
        FormsAuthentication.SignOut();
    }

    public bool Authorize(HttpContextBase httpContext)
    {
        //Check here if the user can administer the  admin
        return false;
    }
}

//The Authorization attribute on a controller
public class CustomAdminAuthorizationFilter : IAuthorizationFilter
{
    private readonly IAuthentication _authentication;

    public SageAdminAuthorizationFilter(IAuthentication authentication)
    {
        _authentication = authentication;
    }

    public void OnAuthorization(AuthorizationContext filterContext)
    {
        _authentication.Authorize(filterContext.HttpContext);
    }
}

我总是收到

  

序列不包含任何元素。

任何帮助都会受到赞赏。

更新: 我仍然收到同样的错误。

调用堆栈如下

找到'c:\ Projects \ Ninject \ ninject.web.mvc \ mvc3 \ src \ Ninject.Web.Mvc \ NinjectHttpApplication.cs'的源代码。校验和:MD5 {3d e3 7f 86 44 70 db 0 3c 6f e0 97 fb 1e 12 13} 文件'c:\ Projects \ Ninject \ ninject.web.mvc \ mvc3 \ src \ Ninject.Web.Mvc \ NinjectHttpApplication.cs'不存在。 查看'c:\ Projects \ Ninject \ ninject.web.mvc \ mvc3 \ src \ Ninject.Web.Mvc \ NinjectHttpApplication.cs'的脚本文档... 查看'c:\ Projects \ Ninject \ ninject.web.mvc \ mvc3 \ src \ Ninject.Web.Mvc \ NinjectHttpApplication.cs'的项目。 在项目中找不到该文件。 查看目录'C:\ Program Files(x86)\ Microsoft Visual Studio 10.0 \ VC \ crt \ src \'... 查看目录'C:\ Program Files(x86)\ Microsoft Visual Studio 10.0 \ VC \ atlmfc \ src \ mfc \'... 查看目录'C:\ Program Files(x86)\ Microsoft Visual Studio 10.0 \ VC \ atlmfc \ src \ atl \'... 查看目录'C:\ Program Files(x86)\ Microsoft Visual Studio 10.0 \ VC \ atlmfc \ include \'... 活动解决方案的调试源文件设置表明调试器不会要求用户找到该文件:c:\ Projects \ Ninject \ ninject.web.mvc \ mvc3 \ src \ Ninject.Web.Mvc \ NinjectHttpApplication.cs。 调试器无法找到源文件'c:\ Projects \ Ninject \ ninject.web.mvc \ mvc3 \ src \ Ninject.Web.Mvc \ NinjectHttpApplication.cs'。

我已经从Nuget安装了Ninject和Ninject.MVC3。它看起来虽然它试图找到要调试的文件。是否有人能够向我提供有关为何发生这种情况的任何信息。

我也收到此错误

[InvalidOperationException:Sequence不包含任何元素]    System.Linq.Enumerable.Single(IEnumerable 1 source) +320 Ninject.Web.Mvc.Bootstrapper.Initialize(Func 1 createKernelCallback)在c:\ Projects \ Ninject \ ninject.web.mvc \ mvc3 \ src \ Ninject.Web.Mvc \ Bootstrapper.cs:67    Ninject.Web.Mvc.NinjectHttpApplication.Application_Start()在c:\ Projects \ Ninject \ ninject.web.mvc \ mvc3 \ src \ Ninject.Web.Mvc \ NinjectHttpApplication.cs:65

2 个答案:

答案 0 :(得分:2)

应使用以下语法注册操作过滤器:

public override void Load()
{
    BindFilter<CustomAdminAuthorizationFilter>(FilterScope.Controller, 0);
    Bind<IAuthentication>().To<CustomAuthenticationService>();
}

您也可以apply the filter conditionally

备注:无法看到您在问题中显示的NinjectDependencyResolver课程的任何关系。这已经内置到ninject.mvc3中,你不应该手动编写它。

答案 1 :(得分:-1)

http://social.msdn.microsoft.com/Forums/en/vsdebug/thread/933f62dd-80e5-4ef3-9199-9e9abfefeadf

通过取消选中

中“要求源文件与原始版本完全匹配”旁边的框来解决
Tools --> Options --> Debugging --> General 

没有科学家的果子