也许我正在以错误的方式接近这个并且应该在动作过滤器中做所有事情,在这种情况下,请指出我正确的方向!
我正在设置我的ASP.NET MVC应用程序,以便一个HomeController Index操作提供两种不同类型的内容,如下所示:
- if(Request.IsAuthenticated)
- return View("IndexRegistered");
- else
- return View("IndexGuest");
这很好,但是我想把它分成三个,所以管理员会得到他们自己的页面……
- if(Request.IsAuthenticated)
- {
- if( /* user is a member of administrators */)
- return View("IndexAdministrator");
- else
- return View("IndexCustomer");
- }
- else
- return View("IndexGuest");
有人可以告诉我这个谜题的遗失吗?
解决方法
使用Authorize
Action Filter的
Roles
属性:
- [Authorize(Roles="Administrators,Moderators")]
- public ActionResult SomeAction(){
- }
或者使用User.IsInRole()方法:
- if(User.IsInRole("Administrator")) { ... }