ASP.NET MVC 2 How to check the user's permissions before action is executed?
我有一个控制器,并且要调用它的所有操作,用户必须拥有这样做的特权。问题是如何在执行动作之前检查?如果用户没有权限,我想呈现带有错误消息的视图。我尝试使用覆盖的
I tried to use overriden OnActionExecuting method, but I can't return a View from that method
事实上你可以:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public override void OnActionExecuting(ActionExecutingContext filterContext) { bool userHasPermissions = CheckUserPermissionsFromSomewhere(filterContext); if (!userHasPermissions) { filterContext.Result = new ViewResult { // you can also specify master page and view model ViewName ="Forbidden" }; } else { base.OnActionExecuting(filterContext); } } |
在Controller类中这个方法是受保护的。