//查询单个(查ID) public int Select(ref M_Online Obj, int ID, ref string ErrMsg) { SqlConnection conn; using (conn = CreatConn()) { SqlCommand cmd = new SqlCommand($"select ID,User_ID,Admin_ID,Admin_BL,Token,LastLogin,PastTime from {TableName} where ID = {ID}", conn); SqlDataReader sdr; try { conn.Open(); sdr = cmd.ExecuteReader(); while (sdr.Read()) { Obj.ID = (int)sdr["ID"]; Obj.Admin_BL = (int)sdr["Admin_BL"]; if (sdr["Admin_ID"] == DBNull.Value) { Obj.Admin_ID = null; } else { Obj.Admin_ID = (int)sdr["Admin_ID"]; } Obj.LastLogin = (DateTime)sdr["LastLogin"]; Obj.PastTime = (long)sdr["PastTime"]; if (sdr["Token"] == DBNull.Value) { Obj.Token = null; } else { Obj.Token = (string)sdr["Token"]; } Obj.User_ID = (int)sdr["User_ID"]; return 1; } return -1; } catch (Exception ex) { ErrMsg = ex.Message; return -1; } finally { cmd.Dispose(); } } }
public override void OnActionExecuting(ActionExecutingContext filterContext) { Random random = new Random(); M_Online obj = new M_Online(); M_TokenTable t_obj = new M_TokenTable(); RefreshToken refreshToken = new RefreshToken(); string ErrMsg = string.Empty; bool bl = false; var user_agent = filterContext.HttpContext.Request.Headers.Get("User-Agent"); var token = filterContext.HttpContext.Session["Token"]; var refroken = filterContext.HttpContext.Request.Cookies["RefreshToken"]; var oid = filterContext.HttpContext.Session["id"]; var uid = filterContext.HttpContext.Session["uid"]; if (uid is null) { uid = filterContext.HttpContext.Request.Cookies["uid"]; //如果Session没有uid就去Cookie拿 } if (uid is null) { filterContext.HttpContext.Response.Redirect("/Home/login"); //如果Cookie没有uid就跳转登录界面 } if (oid is null) { oid = filterContext.HttpContext.Request.Cookies["id"]; //Session没有oid就去Cookie拿 } if (oid is null) { bl = true; } else { obj.ID = (int)oid; int i = (int)oid; if (B_Online.Select(ref obj, obj.ID, ref ErrMsg) != -1) { string tmpstr = RefreshToken.CreateToken(obj.ID, user_agent, obj.Token); if (!string.Equals(tmpstr, token.ToString())) { bl = true; } } } if (refroken is null) { filterContext.HttpContext.Response.Redirect("/Home/login"); } long now = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds; if (now > t_obj.Exp) { filterContext.HttpContext.Response.Redirect("/Home/login"); } if (RefreshToken.AuthenticationRefToken(refroken.Value, t_obj.AesKey, t_obj.AesIV, ref refreshToken, ref ErrMsg)) { base.OnActionExecuting(filterContext); } }
//更新单个(查ID) public int Update(M_Online Obj, ref string ErrMsg) { SqlConnection conn; using (conn = CreatConn()) { SqlCommand cmd = new SqlCommand($"update {TableName} set User_ID = @User_ID, Admin_ID = @Admin_ID, Admin_BL = @Admin_BL, Token = @Token, LastLogin = @LastLogin, PastTime = @PastTime where ID = @ID", conn); SqlParameter par = new SqlParameter("@User_ID", SqlDbType.Int); par.Value = Obj.User_ID; cmd.Parameters.Add(par); par = new SqlParameter("@Admin_ID", SqlDbType.Int); if (Obj.Admin_ID == null) { par.Value = DBNull.Value; } else { par.Value = Obj.Admin_ID; } cmd.Parameters.Add(par); par = new SqlParameter("@Admin_BL", SqlDbType.Int); par.Value = Obj.Admin_BL; cmd.Parameters.Add(par); par = new SqlParameter("@Token", SqlDbType.Char, 50); if (Obj.Token == null) { par.Value = DBNull.Value; } else { par.Value = Obj.Token; } cmd.Parameters.Add(par); par = new SqlParameter("@LastLogin", SqlDbType.DateTime); par.Value = Obj.LastLogin; cmd.Parameters.Add(par); par = new SqlParameter("@PastTime", SqlDbType.BigInt); par.Value = Obj.PastTime; cmd.Parameters.Add(par); par = new SqlParameter("@ID", SqlDbType.Int); par.Value = Obj.ID; cmd.Parameters.Add(par); try { conn.Open(); if (cmd.ExecuteNonQuery() > 0) { return 1; } else { return -1; } } catch (Exception ex) { ErrMsg = ex.Message; return -1; } finally { cmd.Dispose(); } } }
//插入单个 public int Insert(ref M_Online Obj, ref string ErrMsg) { SqlConnection conn; using (conn = CreatConn()) { SqlCommand cmd = new SqlCommand($"insert into {TableName} (User_ID,Admin_ID,Admin_BL,Token,LastLogin,PastTime) values (@User_ID,@Admin_ID,@Admin_BL,@Token,@LastLogin,@PastTime);select @@IDENTITY as int", conn); SqlParameter par = new SqlParameter("@User_ID", SqlDbType.Int); par.Value = Obj.User_ID; cmd.Parameters.Add(par); par = new SqlParameter("@Admin_ID", SqlDbType.Int); if (Obj.Admin_ID == null) { par.Value = DBNull.Value; } else { par.Value = Obj.Admin_ID; } cmd.Parameters.Add(par); par = new SqlParameter("@Admin_BL", SqlDbType.Int); par.Value = Obj.Admin_BL; cmd.Parameters.Add(par); par = new SqlParameter("@Token", SqlDbType.Char, 50); if (Obj.Token == null) { par.Value = DBNull.Value; } else { par.Value = Obj.Token; } cmd.Parameters.Add(par); par = new SqlParameter("@LastLogin", SqlDbType.DateTime); par.Value = Obj.LastLogin; cmd.Parameters.Add(par); par = new SqlParameter("@PastTime", SqlDbType.BigInt); par.Value = Obj.PastTime; cmd.Parameters.Add(par); try { conn.Open(); decimal d = (decimal)cmd.ExecuteScalar(); Obj.ID = (int)d; if (Obj.ID > 0) { return 1; } else { return -1; } } catch (Exception ex) { ErrMsg = ex.Message; return -1; } finally { cmd.Dispose(); } } }
//查询多个(查ID) public int Select(ref List<M_Online> Obj, List<int> ID, ref string ErrMsg) { SqlConnection conn; using (conn = CreatConn()) { SqlCommand cmd = new SqlCommand($"select ID,User_ID,Admin_ID,Admin_BL,Token,LastLogin,PastTime from {TableName} where ID = @ID", conn); SqlDataReader sdr; SqlParameter par = new SqlParameter("@ID", SqlDbType.Int); cmd.Parameters.Add(par); int tmpOut = 0; try { conn.Open(); foreach (int i in ID) { cmd.Parameters["@ID"].Value = i; sdr = cmd.ExecuteReader(); while (sdr.Read()) { M_Online TmpObj = new M_Online(); TmpObj.ID = (int)sdr["ID"]; TmpObj.Admin_BL = (int)sdr["Admin_BL"]; if (sdr["Admin_ID"] == DBNull.Value) { TmpObj.Admin_ID = null; } else { TmpObj.Admin_ID = (int)sdr["Admin_ID"]; } TmpObj.LastLogin = (DateTime)sdr["LastLogin"]; TmpObj.PastTime = (long)sdr["PastTime"]; if (sdr["Token"] == DBNull.Value) { TmpObj.Token = null; } else { TmpObj.Token = (string)sdr["Token"]; } TmpObj.User_ID = (int)sdr["User_ID"]; Obj.Add(TmpObj); tmpOut++; } sdr.Close(); } } catch (Exception ex) { ErrMsg = ex.Message; return -1; } finally { cmd.Dispose(); } if (ID.Count() == tmpOut) { return 1; } else { return -1; } } }
public ActionResult Login(Models.LoginViewModel model) { Random r = new Random(); RefreshToken rt = new RefreshToken(); M_TokenTable mtt = new M_TokenTable(); M_EmployeeLogin obj = new M_EmployeeLogin(); M_Online mol = new M_Online(); string ErrMsg = string.Empty; if (B_EmployeeLogin.Login(ref obj, model.LoginTel, model.Password, ref ErrMsg) != -1) { try { mol.PastTime = (long)DateTime.UtcNow.AddMinutes(30).Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds; mol.LastLogin = DateTime.Now; mol.Admin_BL = 1; mol.Token = r.NextDouble().ToString(); B_Online.Insert(ref mol, ref ErrMsg); rt.iss = "lzfyhgm"; rt.exp = (long)DateTime.UtcNow.AddDays(30).Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds; rt.iat = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;; byte[] key = AES.Md5((rt.exp * r.NextDouble()).ToString(), AES.Md5DataType.t32); byte[] IV = AES.Md5((rt.iat * r.NextDouble()).ToString(), AES.Md5DataType.t16); mtt.AesIV = Convert.ToBase64String(key); mtt.AesKey = Convert.ToBase64String(IV); mtt.Exp = rt.exp; mtt.User_ID = obj.ID; mtt.Sign = RefreshToken.Sign(rt); B_TokenTable.Insert(ref mtt, ref ErrMsg); string refreshtoken = RefreshToken.CreateReftoken(rt, key, IV, obj.ID); string user_agent = HttpContext.Request.Headers.Get("User-Agent"); string token = RefreshToken.CreateToken(obj.ID, user_agent, mol.Token); UseCookie.Add("UserName", model.LoginTel, DateTime.Now.AddMinutes(30)); UseSession.Add("UserName", model.LoginTel); UseCookie.Add("UserID", obj.ID.ToString(), DateTime.Now.AddMinutes(30)); UseSession.Add("UserID", obj.ID.ToString()); UseCookie.Add("token", token, DateTime.Now.AddMinutes(30)); UseSession.Add("token", token); UseCookie.Add("RefreshToken", refreshtoken, DateTime.Now.AddDays(30)); } catch (Exception ex) { Response.Write(ex.Message); return(View()); } return(RedirectToAction("Index")); } Response.Write(ErrMsg); return(View()); }
//查询单个(查ID) public static int Select(ref M_Online Obj, int ID, ref string ErrMsg) { I_Online I = (D_Online)SimpleFactory.CreateObject(DBType.Online); return I.Select(ref Obj, ID, ref ErrMsg); }
//更新单个(查ID) public static int Update(M_Online Obj, ref string ErrMsg) { I_Online I = (D_Online)SimpleFactory.CreateObject(DBType.Online); return I.Update(Obj, ref ErrMsg); }