开发者

How can I return json data and then stop the corresponding action in my custom filter attribution ?

开发者 https://www.devze.com 2023-03-27 00:38 出处:网络
There is an a开发者_运维问答synchronous request that need authorize user. I want to authorize in filter attribution. If user can\'t login, return a json data that used to tell the client to callback a

There is an a开发者_运维问答synchronous request that need authorize user. I want to authorize in filter attribution. If user can't login, return a json data that used to tell the client to callback a javascript function to popup the login window. how can I stop the action in filter attribution?


Create a custom ActionFilterAttribute. Override OnActionExecuting.

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
    {
        filterContext.Result = new EmptyResult();
        return;
    }

    base.OnActionExecuting(filterContext);
}

Instead of EmptyResult return whatever you want.

0

精彩评论

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