Exemplo n.º 1
0
        void Create(string _sessionId = "")
        {
            if (_sessionId != "")
            {
                sessionId = _sessionId;
            }
            if (sessionId == "")
            {
                string a1 = "";
                string a  = M5.PageContext.Current.Request.Cookies["M5_SessionId"];
                M5.PageContext.Current.Request.Cookies.TryGetValue("M5_SessionId", out string value);
                sessionId = Tools.GetId();
                Microsoft.AspNetCore.Http.CookieOptions option = new Microsoft.AspNetCore.Http.CookieOptions();
                option.Expires = new DateTimeOffset(DateTime.Now.AddDays(1));
                M5.PageContext.Current.Response.Cookies.Append("M5_SessionId", sessionId, option);
            }

            /*
             * MySqlDataReader rs2 = Sql.ExecuteReader("select A.userid,A.logindate from logininfo A where   A.sessionId=@sessionId and logindate>@loginDate)",
             *  new MySqlParameter[] {
             *      new MySqlParameter("sessionId", sessionId),
             *      new MySqlParameter("loginDate",DateTime.Now.AddHours(-1) )
             * });*/
            MySqlDataReader rs2 = Sql.ExecuteReader("select A.userid,A.logindate from logininfo A where   A.sessionId=@sessionId and logindate>@loginDate",
                                                    new MySqlParameter[] {
                new MySqlParameter("sessionId", sessionId),
                new MySqlParameter("loginDate", DateTime.Now.AddHours(-1))
            });

            if (rs2.Read())
            {
                value = UserClass.get(rs2.GetDouble(0));
                DateTime loginDate = rs2.GetDateTime(1);
                if ((System.DateTime.Now - loginDate).TotalMinutes > 40)
                {//间隔40分钟更新一次
                    Sql.ExecuteNonQuery("update logininfo set logindate=@logindate where sessionId=@sessionId", new MySqlParameter[] {
                        new MySqlParameter("sessionId", sessionId),
                        new MySqlParameter("logindate", DateTime.Now)
                    });
                }
            }
            rs2.Close();
        }
Exemplo n.º 2
0
        public static ReturnValue login(double userId, int hour)
        {
            string      ip   = Tools.IPToNumber("127.0.0.1").ToString();//  Tools.IPToNumber(M5.PageContext.Current.Request.UserHostAddress).ToString();
            ReturnValue err  = new ReturnValue();
            UserInfo    info = UserClass.get(userId);

            /*if (!info.ipAccess(M5.PageContext.Current.Request.UserHostAddress))
             * {
             *  err.errNo = -1;
             *  err.errMsg = "您的ip不能登陆";
             *  return err;
             * }*/
            err.errNo = 0;
            //添加登陆信息
            //添加登陆日志
            string sessionId = (M5.PageContext.Current.Request.Cookies["M5_SessionId"] == null) ? "" : M5.PageContext.Current.Request.Cookies["M5_SessionId"];

            if (sessionId == "")
            {
                sessionId = Tools.GetId();
                M5.PageContext.Current.Response.Cookies.Append("M5_SessionId", sessionId);
            }
            Sql.ExecuteNonQuery("update m_admin set loginDateTime=loginDateTime where id=@id", new MySqlParameter[] {
                new MySqlParameter("id", userId),
                new MySqlParameter("loginDateTime", DateTime.Now),
            });
            Sql.ExecuteNonQuery("delete from logininfo where logindate<@logindate or sessionId=@sessionId", new MySqlParameter[] {
                new MySqlParameter("sessionId", sessionId),
                new MySqlParameter("logindate", DateTime.Now.AddHours(-1)),
            });        //删除超时用户及重登陆用户
            Sql.ExecuteNonQuery("insert logininfo (sessionId,ip,logindate,userid)values(@sessionId,@ip,@logindate,@userId)",
                                new MySqlParameter[] {
                new MySqlParameter("sessionId", sessionId),
                new MySqlParameter("ip", ip),
                new MySqlParameter("userId", userId),
                new MySqlParameter("logindate", DateTime.Now),
            });
            err.userData = info;
            if (info.classId == 0)
            {
                M5.PageContext.Current.Response.Cookies.Append("M5_Login", "true");
            }
            else
            {
                M5.PageContext.Current.Response.Cookies.Append("M5_Login", "");
            }
            M5.PageContext.Current.Response.Cookies.Append("u_name", HttpUtility.UrlEncode(info.username));
            M5.PageContext.Current.Response.Cookies.Append("u_id", info.id.ToString());

            /*HttpCookie cook = new HttpCookie("u_name");
             * if (hour > 0) cook.Expires = System.DateTime.Now.AddHours(hour);
             * cook.Value = HttpUtility.UrlEncode(info.username);
             * PageContext.Current.Response.Cookies.Add(cook);
             * cook = new HttpCookie("u_id");
             * if (hour > 0) cook.Expires = System.DateTime.Now.AddHours(hour);
             * cook.Value = info.id.ToString();
             * PageContext.Current.Response.Cookies.Add(cook);*/

            //PageContext.Current.Response.Cookies["u_name"].Value = HttpUtility.UrlEncode(info.username);
            //PageContext.Current.Response.Cookies["u_id"].Value = info.id.ToString();
            Tools.writeLog("login", info.username + "登陆成功");
            return(err);
        }