Use ActionFilterAttribute to check user logging

public class CheckLoginAttribute : ActionFilterAttribute
    {
        public CheckLoginAttribute()
        {
 
        }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.HttpContext.Session["User"] == null)
            {
                filterContext.Result = new RedirectResult("/Account/LogOn");
                return;
            }
            base.OnActionExecuting(filterContext);
        }

    }
public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            return View();
        }
        [CheckLogin]
        public ActionResult About()
        {
            return View();
        }
    }

ref: MSDN

posted on 2012-04-11 23:50  笑东风  阅读(227)  评论(0编辑  收藏  举报

导航