自定义一个授权筛选器类,继承于AuthorizeAttribute:

using System;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1
{
    public class DWAuthorizeAttribute : AuthorizeAttribute
    {
        /// <summary>
        /// 判断用户是否登录
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            HttpContextBase context = filterContext.HttpContext;
            if (context.Session["LoginUser"] == null)
                context.Response.Redirect("~/User/Login");
        }
    }
}

 在需进行登录验证的Action上加约束:

[DWAuthorize]
public ActionResult Index()
{
    return View();
}

 

posted on 2014-05-04 17:38  松竹梅  阅读(960)  评论(1编辑  收藏  举报