CheckLogin

-----------------------------------------------下面是使用过的------------------------------------------------

 BaseController内部

public class CheckLogin : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{

if (filterContext.HttpContext.Session != null)
{
if (filterContext.HttpContext.Session.IsNewSession)
{
var sessionCookie = filterContext.HttpContext.Request.Headers["Cookie"];
if ((sessionCookie != null) && (sessionCookie.IndexOf("ASP.NET_SessionId", StringComparison.OrdinalIgnoreCase) >= 0))
{
//filterContext.HttpContext.Response.Write("<script type=\"text/javascript\">top.location.href='/Home/LoginOut';</script>");
Logon(filterContext);
}
else
{
int Role = BaseController.GetSession().RoleID;
string Url = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName + "/" + filterContext.ActionDescriptor.ActionName;
SysRole role = new SysRole();
if (role.ValidatePage(Url, Role))
{
//filterContext.HttpContext.Response.Write("<script type=\"text/javascript\">top.location.href='/Home/LoginOut';</script>");
Logon(filterContext);
}
}
}

}
}


/// <summary>
/// 路由到登录页面
/// </summary>
/// <param name="filterContext"></param>
private void Logon(ActionExecutingContext filterContext)
{
RouteValueDictionary dictionary = new RouteValueDictionary
(new
{
controller = "Home",
action = "LoginOut",
returnUrl = filterContext.HttpContext.Request.RawUrl
});
filterContext.Result = new RedirectToRouteResult(dictionary);
}
}

-----------------------------------------下面这段是摘抄-----------------------------------------

1、直接重载当前的控制器就可以。整个站点需要,当然可以创建一个Base控制器。

protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var timestamp = filterContext.HttpContext.Timestamp;
            var timeout = filterContext.HttpContext.Session.Timeout;

            var userSessionID = Session["UserSessionID"];
            var user = Session["User"];
            if (userSessionID == null || user == null)
            {
                Logon(filterContext);
            } 
        }

        private void Logon(ActionExecutingContext filterContext )
        {
            RouteValueDictionary dictionary = new RouteValueDictionary
            (new
            {
                controller = "Account",
                action = "Logon",
                returnUrl = filterContext.HttpContext.Request.RawUrl
            });
            filterContext.Result = new RedirectToRouteResult(dictionary);
           }
       }

-----------------------------------------上面这段是摘抄-----------------------------------------

public class CheckSessionFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpContext httpcontext = HttpContext.Current; //获取当前的 HttpSessionState   
            var userSessionID = httpcontext.Session["UserSessionID"];
            if (userSessionID == null)
            {
                Logon(filterContext);
            }
            if( httpcontext.Session != null )            
            {             
                //确认Session是否已建立            
                if( httpcontext.Session.IsNewSession )       
                {                                      
                    //確認是否已存在cookies                 
                    String sessioncookie = httpcontext.Request.Headers[ "Cookie" ];             
                    if( (sessioncookie != null ) && ( sessioncookie.IndexOf( "ASP.NET_SessionId" ) >= 0 ))
                    {      
                        Logon( filterContext );               
                    }            
                }            
            }          
            base.OnActionExecuting( filterContext );      
        }

        /// <summary>
        /// 路由到登录页面
        /// </summary>
        /// <param name="filterContext"></param>
        private void Logon(ActionExecutingContext filterContext )
        {
            RouteValueDictionary dictionary = new RouteValueDictionary
            (new
            {
                controller = "Account",
                action = "Logon",
                returnUrl = filterContext.HttpContext.Request.RawUrl
            });
            filterContext.Result = new RedirectToRouteResult(dictionary);
           }
       }

posted @ 2013-12-04 15:41  JIN__JIN  阅读(1311)  评论(0编辑  收藏  举报