在MVC4中检查控制器内的授权(角色)

use*_*150 1 asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

我在项目中创建了一个控制器.

         [Authorize(Roles = "Admin")]
        private StudentRepositor obj = new StudentRepositor();
        public ActionResult Index()
        {

            var model = obj.GetStudentlist();
            foreach (var stu in model)
            {
                stu.State = (stu.State == "1") ? "????" : "??????? ";
            }
            return View(model);
        }
Run Code Online (Sandbox Code Playgroud)

我想检查控制器内部的权限,而不是外部.

例如,像这样的事情:

 public ActionResult Index()
            {

               if(Role=admin) return view2
               if(role=teacher) return view1
            }
Run Code Online (Sandbox Code Playgroud)

我可以这样做吗?!!

最好的祝福

Tob*_*ias 6

你应该可以使用 User.IsInRole()

 public ActionResult Index()
        {

           if(User.IsInRole("admin")) 
           {
               //Return View
           }
           else if(User.IsInRole("teacher")) 
           {
               //Return View
           }
           else
           {
               //Return View
           }
        }
Run Code Online (Sandbox Code Playgroud)