Best way to abort/cancel action and response from ActionFilter

后端 未结 6 1647
清歌不尽
清歌不尽 2020-12-24 13:51

Best way to abort/cancel action from ActionFilter

I\'ve got this ActionFilter, and it\'s suppose to end the connection immediately and return a 401 Unau

6条回答
  •  悲哀的现实
    2020-12-24 14:30

    You probably want to make it an AuthorizeAttribute. That will set the result to be an UnAuthorizedResult automatically, plus it has the benefit of being run before any other filters. Alternatively you can set the Result to be a new HttpUnauthorizedResult

    public class SignInRequiredAttribute : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            return !Acme.Web.CurrentUser != null;
        }
    }
    

提交回复
热议问题