Currently I am trying to set up dependency injection and override the authorisation attribute. I have tried to follow a few examples although I always seem to 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 receive
Sequence contains no elements.
Any help would appreciated.
Update: I am still receiving the same error.
The call stack is as follows
Locating 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} The file 'c:\Projects\Ninject\ninject.web.mvc\mvc3\src\Ninject.Web.Mvc\NinjectHttpApplication.cs' does not exist. Looking in script documents for 'c:\Projects\Ninject\ninject.web.mvc\mvc3\src\Ninject.Web.Mvc\NinjectHttpApplication.cs'... Looking in the projects for 'c:\Projects\Ninject\ninject.web.mvc\mvc3\src\Ninject.Web.Mvc\NinjectHttpApplication.cs'. The file was not found in a project. Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\crt\src\'... Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\atlmfc\src\mfc\'... Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\atlmfc\src\atl\'... Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\atlmfc\include\'... The debug source files settings 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 locate the source file 'c:\Projects\Ninject\ninject.web.mvc\mvc3\src\Ninject.Web.Mvc\NinjectHttpApplication.cs'.
I have installed Ninject and Ninject.MVC3 from Nuget. It looks although it is trying to find the files to debug. Would anyone be able to give me any information to why this is happening.
I also receive this error
[InvalidOperationException: Sequence contains no elements]
System.Linq.Enumerable.Single(IEnumerable1 source) +320
Ninject.Web.Mvc.Bootstrapper.Initia开发者_运维技巧lize(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
Action filters should 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.
Remark: can't see any relation with the NinjectDependencyResolver
class you are showing in your question. That's already built into ninject.mvc3 and you shouldn't be writing it manually.
http://social.msdn.microsoft.com/Forums/en/vsdebug/thread/933f62dd-80e5-4ef3-9199-9e9abfefeadf
resolved by un-checking the box next to “Require source files to exactly match the original version” in
Tools --> Options --> Debugging --> General
No fruitcup for scientologists
精彩评论