0

我有这个过滤器类。

 public class Sessional : ActionFilterAttribute
    {    
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpSessionStateBase session = filterContext.HttpContext.Session;
            LoggedUserInfo user = (LoggedUserInfo)session["User"];

            if ((user == null && !session.IsNewSession) || (session.IsNewSession))
            {
                UrlHelper urlHelper = new UrlHelper(filterContext.RequestContext);
                string loginUrl = urlHelper.Content("~/Account/LogOut");
                FAuth.AbandonSession();
                FormsAuthentication.SignOut();
                filterContext.HttpContext.Response.Redirect(loginUrl, true);
            }
        }
    }

当我在控制器上应用它时,如果会话不可用,所有操作都将注销用户。但我想编写允许唯一操作在不注销的情况下完成其工作的属性,例如 UnSessional。

 [Authorize] 
    [Sessional]
    public class ReportController : Controller
    {
        [HttpGet] 
        [UnSessional]
        public ActionResult GetReport() //unsessional action
        {
            return View();
        }

        [HttpPost]
        public ActionResult GetReport(GetReportModel model) //sessional action
        {
            if (!ModelState.IsValid)
            {
                return View();
            }
            return View();
        }
    }
4

1 回答 1

2

您可以检查当前操作的“UnSessionAttribute”是否存在,这是示例代码:

 public override void OnActionExecuting(ActionExecutingContext filterContext)
        {

            if(filterContext.ActionDescriptor.GetCustomAttributes(typeof(UnSessionAttribute), true).Length > 0)
            {
                  return;
            }

            HttpSessionStateBase session = filterContext.HttpContext.Session;
            LoggedUserInfo user = (LoggedUserInfo)session["User"];

            if ((user == null && !session.IsNewSession) || (session.IsNewSession))
            {
                UrlHelper urlHelper = new UrlHelper(filterContext.RequestContext);
                string loginUrl = urlHelper.Content("~/Account/LogOut");
                FAuth.AbandonSession();
                FormsAuthentication.SignOut();
                filterContext.HttpContext.Response.Redirect(loginUrl, true);
            }
        }
于 2013-07-15T11:14:51.110 回答