开发者

ASP.NET MVC 2 How to check the user's permissions before action is executed?

I have a controller, and to invoke all its actions the user has to ha开发者_Go百科ve privilages to do that. The question is how to check that before action is executed? If the user doesn't have permissions I want to render a View with error message. I tried to use overriden OnActionExecuting method, but I can't return a View from that method


I tried to use overriden OnActionExecuting method, but I can't return a View from that method

As a matter of fact you can:

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);
    }
}


In the class Controller this method is protected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜