我创建了一个 ASP.Net MVC Web 应用程序并使用 NuGet 安装了 Ninject 2.2.1.4 版和 Ninject.MVC3 2.2.2.0 版。此应用程序使用表单身份验证。我在授权工作时遇到问题...
这是我在 Global.asax 中的 Application_Start 方法:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
var kernel = new StandardKernel();
// binding code goes here...
// Remove this dependency resolver and Authorisation will work...
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
}
我在控制器中的一项操作:
[Authorize(Roles = "Admin")]
public ActionResult About()
{
return View();
}
我的 web.config 的身份验证部分:
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>
问题是未经授权的用户直接进入 About 视图,而不是被重定向到 LogOn。我知道问题出在 NinjectDependencyResolver 上,因为当我从 Application_Start 中删除“DependencyResolver.SetResolver ...”行时,未经授权的用户被重定向。
我会很感激你的帮助。
问候,菲尔