• 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);
               }
           }

  • 相关阅读:
    LeetCode算法题-Convert Sorted Array to Binary Search Tree(Java实现)
    LeetCode算法题-Binary Tree Level Order Traversal II(Java实现)
    LeetCode算法题-Maximum Depth of Binary Tree
    LeetCode算法题-Symmetric Tree(Java实现)
    LeetCode算法题-Same Tree(Java实现)
    基于任务的异步编程模式,Task-based Asynchronous Pattern
    Nito.AsyncEx 这个库
    暴力 六点钟
    夜晚 十点 React-Native 源码 暴力畜 系列
    夜晚 暴力 十点钟 jQuery 的 extend 实现 原理
  • 原文地址:https://www.cnblogs.com/jcz1206/p/3457706.html
Copyright © 2020-2023  润新知