ASP.NET MVC3 IAuthorisationfilter Ninject Dependency

I'm currently trying to set up a dependency nesting and override the authorization attribute. I tried to follow a few examples, although I always get errors.

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; } } 

Authentication filter

 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); } } 

I always get

The sequence contains no elements.

Any help would be appreciated.

Update: I am still getting the same error.

The call stack is as follows

Search for a source for 'c: \ Projects \ Ninject \ ninject.web.mvc \ mvc3 \ src \ Ninject.Web.Mvc \ NinjectHttpApplication.cs'. Checksum: MD5 {3d e3 7f 86 44 70 db 0 3c 6f e0 97 fb 1e 12 13} File 'c: \ Projects \ Ninject \ ninject.web.mvc \ mvc3 \ src \ Ninject.Web.Mvc \ NinjectHttpApplication.cs ' does not exist. Viewing documents in a script for 'c: \ Projects \ Ninject \ ninject.web.mvc \ mvc3 \ src \ Ninject.Web.Mvc \ NinjectHttpApplication.cs' ... In projects for' c: \ Projects \ Ninject \ ninject.web .mvc \ mvc3 \ src \ Ninject.Web.Mvc \ NinjectHttpApplication.cs'. File not found in project. Browse in the directory 'C: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ VC \ crt \ src \' ... Browse in the directory 'C: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ VC \ atlmfc \ src \ mfc \ '... View in the directory' C: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ VC \ atlmfc \ src \ atl \ '... View in the directory' C: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ VC \ atlmfc \ include \ '... The parameters of the debugging source files for the active solution indicate that the debugger will not ask the user to find the file: c: \ Projects \ Ninject \ ninject.web.mvc \ mvc3 \ src \ Ninject.Web.Mvc \ NinjectHttpApplication.cs. The debugger could not find the source file 'c: \ Projects \ Ninject \ ninject.web.mvc \ mvc3 \ src \ Ninject.Web.Mvc \ NinjectHttpApplication.cs'.

I installed Ninject and Ninject.MVC3 from Nuget. It looks though it is trying to find files for debugging. Can anyone give me information on why this is happening.

I also get this error

[InvalidOperationException: The sequence contains no elements] System.Linq.Enumerable.Single (IEnumerable 1 source) +320 Ninject.Web.Mvc.Bootstrapper.Initialize(Func 1 createKernelCallback) in c: \ Projects \ Ninject \ ninject.web.mvc \ mvc3 \ src \ Ninject.Web.Mvc \ Bootstrapper.cs: 67 Ninject.Web.Mvc.NinjectHttpApplication.Application_Start () in c: \ Projects \ Ninject \ ninject.web.mvc \ mvc3 \ src \ Ninject.Web.Mvc \ NinjectHttpApplication.cs: 65

+4
source share
2 answers

Action filters must be registered using the following syntax:

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

You can also apply the filter conditionally .

Note: I do not see any relation to the NinjectDependencyResolver class that you show in your question. This is already built into ninject.mvc3 and you should not write it manually.

+2
source

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

allowed by checking the box next to "Require source files to match the original version" in

 Tools --> Options --> Debugging --> General 

No fruit dolls for Scientologists

-1
source

Source: https://habr.com/ru/post/1347717/


All Articles