从onAuthorization CustomAttribute MVC返回字符串内容

时间:2015-07-20 10:11:39

标签: c# asp.net asp.net-mvc authorization authorize-attribute

我有自定义authorize属性,如果结果中的授权失败,我必须发送一个字符串。我想在结果中发送字符串

  

filterContext.Result =" Some String Here&#34 ;;

所以,ajax会读取并做一些事情。

public class PermissionAuthorizeAttribute : AuthorizeAttribute
{

    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        if (!isAuthorized(user, controllerName, actionName))
                    {
                        if (filterContext.HttpContext.Request.IsAjaxRequest())
                        {
                            //I want to retur here string content like.
                            filterContext.Result = "UnauthorizedAccess";
                        }
                        else
                        {
                            filterContext.Result = new ViewResult() { ViewName = "_Unauthorized" };
                        }
                    }
    }

    //function for checking if the user is authorized to access the page 
    private bool isAuthorized(string username, string controller, string action)
    {
        bool authorized = false;
        return authorized;
    }
}

0 个答案:

没有答案