How to get filter to redirect to another action?

后端 未结 5 931
伪装坚强ぢ
伪装坚强ぢ 2020-12-13 07:52

RedirectToAction is protected, and we can use it only inside actions. But if I want to redirect in a filter?

public class IsGuestAttribute: Acti         


        
5条回答
  •  南方客
    南方客 (楼主)
    2020-12-13 08:42

    Security/Authorization/Authentication Filters should use the AuthorizeAttribute and IAuthorizationFilter.

    public class IsGuestAttribute: AuthorizeAttribute, IAuthorizationFilter
    {
        public void OnResultExecuted(ResultExecutedContext filterContext)
        {
        }
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (!Ctx.User.IsGuest) 
            {
                filterContext.Result = new RedirectToRouteResult(
                    new RouteValueDictionary 
                    { 
                        { "controller", "Home" }, 
                        { "action", "Index" } 
                    });
            }
        }
    }
    

提交回复
热议问题