login.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. using BizCom;
  2. using SiteCore.Redis;
  3. using SQLData;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Reflection;
  8. using System.Text.RegularExpressions;
  9. using System.Web;
  10. using System.Web.SessionState;
  11. using Utils;
  12. namespace SiteCore.Handler
  13. {
  14. public partial class login : BaseHandler, IHttpHandler, IRequiresSessionState
  15. {
  16. public void ProcessRequest(HttpContext context)
  17. {
  18. //if (CurrentUser == null)
  19. //{
  20. // ReturnLoginMsg("操作己过期,请重新登录!");
  21. // return;
  22. //}
  23. con = context;
  24. string methodName = GetString("t");
  25. Type type = this.GetType();
  26. MethodInfo method = type.GetMethod(methodName);
  27. if (method == null)
  28. throw new Exception("method is null");
  29. if (isLoginOut())
  30. {
  31. if (methodName != "erp_user_login" && methodName != "loginout" && methodName != "file_client_down_url")
  32. {
  33. returnErrorMsg("操作己过期,请重新登录!");
  34. return;
  35. }
  36. }
  37. try
  38. {
  39. method.Invoke(this, null);
  40. if (CurrentUser != null)
  41. {
  42. LogOperate.Save(CurrentUser.UserID, CurrentUser.UserName, CurrentUser.User.LoginIP, methodName);
  43. }
  44. /*if (operate_Tag != "") WebLog.SysLog(CurrentUser.UserID,operate_Tag);
  45. else WebLog.SysLog(CurrentUser.UserID, methodName);*/
  46. }
  47. catch (Exception ex)
  48. {
  49. //WebLog.SysLog(CurrentUser.UserID, ex);
  50. returnErrorMsg("操作失败!" + ex.Message);
  51. }
  52. //if (methodName.IndexOf("get_") == -1)
  53. //{
  54. // using (new SessionScope())
  55. // {
  56. // }
  57. //}
  58. //else
  59. //{
  60. // try
  61. // {
  62. // method.Invoke(this, null);
  63. // }
  64. // catch (Exception ex)
  65. // {
  66. // XLog.SaveLog(CurrentUser.UserID, ex.Message);
  67. // returnErrorMsg("操作失败!" + ex.Message);
  68. // }
  69. //}
  70. }
  71. public void isAccLogin()
  72. {
  73. HttpCookie lcCookie = HttpContext.Current.Request.Cookies[webConfig.CookieName];
  74. if (lcCookie != null)
  75. {
  76. if (CurrentUser != null && CurrentUser.User.IsMultiLogin == 1)
  77. {
  78. returnSuccessMsg("1");
  79. return;
  80. }
  81. string name = HttpUtility.UrlDecode(lcCookie.Values["user"]);
  82. string ticket = lcCookie.Values["ticket"];
  83. string dl_ticket = erpRedis.RedisHelper.StringGet("dl_" + name);
  84. if (ticket != dl_ticket)
  85. returnSuccessMsg("0");
  86. else
  87. returnSuccessMsg("1");
  88. //if (String.IsNullOrEmpty(dl_ticket))
  89. //{
  90. // erpRedis.RedisHelper.StringSet("dl_" + name, ticket);
  91. // returnSuccessMsg("1");
  92. //}
  93. //else
  94. //{
  95. //}
  96. return;
  97. }
  98. returnSuccessMsg("0");
  99. }
  100. public void trans_user_role()
  101. {
  102. if (UrlPostParmsCheck("r"))
  103. {
  104. string user = CurrentUser.User.Account;
  105. string pwd = CurrentUser.User.PassWord;
  106. user = user.Split('_')[0];
  107. int r = GetPostInt("r");
  108. switch (r)
  109. {
  110. case 1: user = user + "_kf"; break;
  111. case 2: user = user + "_sj"; break;
  112. case 3: user = user + "_sh"; break;
  113. case 4: user = user + "_cw"; break;
  114. default: break;
  115. }
  116. CeErpUser entity = CeErpUser.GetByLogin(user, pwd);
  117. if (entity != null)
  118. {
  119. entity.LoginIP = CommonHelper.ClientIP;
  120. entity.Ticket = WebHelper.getLoginTicket(entity.Account, entity.LoginIP);
  121. if (entity.CurLoginTime.ToString() != "")
  122. entity.PreLoginTime = Convert.ToDateTime(entity.CurLoginTime);
  123. entity.CurLoginTime = DateTime.Now;
  124. entity.Update();
  125. HttpCookie cookie = new HttpCookie(webConfig.CookieName);
  126. cookie.Values.Clear();
  127. cookie.Values.Add("user", user);
  128. cookie.Values.Add("ticket", entity.Ticket);
  129. cookie.Path = "/";
  130. //cookie.Domain = SiteDomain;
  131. //if (chkWeek.Checked) cookie.Expires = DateTime.Now.AddDays(14);
  132. cookie.Expires = DateTime.Now.AddDays(2);
  133. HttpContext.Current.Response.Cookies.Add(cookie);
  134. returnSuccessMsg(HttpUtility.UrlEncode(SecurityHelper.EncryptSymmetric(user + "|" + DateTime.Now.ToString("yyyy-MM-dd"))));
  135. return;
  136. }
  137. }
  138. returnErrorMsg("无法切换,可能没有该角色!");
  139. }
  140. public static FrequencyControler LoginFrequency = new FrequencyControler("erplogin", 10, 4);
  141. public void erp_user_login()
  142. {
  143. if (LoginFrequency.IsTooFrequently(true))
  144. {
  145. returnErrorMsg("访问太频繁");
  146. return;
  147. }
  148. //XLog.SaveLog(0, "123");
  149. if (!UrlPostParmsCheck("user,pwd"))
  150. {
  151. returnErrorMsg("参数有误!");
  152. return;
  153. }
  154. string account = GetPostString("user");
  155. string pwd = GetPostString("pwd");
  156. if (account == "" || pwd == "")
  157. {
  158. returnErrorMsg("请输入用户名或密码!");
  159. return;
  160. }
  161. string code = GetPostString("code");
  162. if (con.Session["vCode"] != null)
  163. {
  164. if (code == "" || code.ToLower() != con.Session["vCode"].ToString().ToLower())
  165. {
  166. returnErrorMsg("验证码不正确!");
  167. return;
  168. }
  169. }
  170. CeErpUser entity = CeErpUser.GetByLogin(account, SecurityHelper.EncryptSymmetric(pwd));
  171. string ip = WebHelper.GetIP();
  172. if (SiteInfo.isPassIp(ip) == false)
  173. {
  174. if (!(entity != null && entity.State == 5))
  175. {
  176. returnErrorMsg("未授权的IP:" + ip);
  177. return;
  178. }
  179. }
  180. if (!string.IsNullOrEmpty(erpRedis.RedisHelper.StringGet("lock_" + account)))
  181. {
  182. returnErrorMsg("账户于" + erpRedis.RedisHelper.StringGet("lock_" + account) + "锁定!");
  183. return;
  184. }
  185. //XLog.SaveLog(0, "456");
  186. if (entity == null)
  187. {
  188. if (con.Session["lerr" + account] != null)
  189. con.Session["lerr" + account] = Convert.ToInt32(con.Session["lerr" + account]) + 1;
  190. else
  191. con.Session["lerr" + account] = 1;
  192. con.Session["lerr"] = con.Session["lerr" + account];
  193. string errMsg = "";
  194. if (Convert.ToInt32(con.Session["lerr"]) > 5)
  195. {
  196. erpRedis.RedisHelper.StringSet("lock_" + account, DateTime.Now.ToLongTimeString(), new TimeSpan(0, 10, 0));
  197. errMsg = con.Session["lerr"] + "|账号或密码不正确!账户锁定10分钟。";
  198. }
  199. else
  200. {
  201. errMsg = con.Session["lerr"] + "|账号或密码不正确!";
  202. }
  203. Log_Login.Save(account, ip, false, errMsg);
  204. returnErrorMsg(errMsg);
  205. return;
  206. }
  207. if (entity.State == 1)
  208. {
  209. returnErrorMsg("账户已被冻结!");
  210. Log_Login.Save(account, ip, false, "账户已被冻结!");
  211. return;
  212. }
  213. entity.LoginIP = ip;
  214. entity.Ticket = WebHelper.getLoginTicket(entity.Account + DateTime.Now.ToString("yyyyMMddHHmmssffff"));
  215. if (entity.CurLoginTime.ToString() != "")
  216. {
  217. entity.PreLoginTime = Convert.ToDateTime(entity.CurLoginTime);
  218. }
  219. entity.CurLoginTime = DateTime.Now;
  220. //查询上次登录时间是否为昨天,一天第一次登录清空设计师派单数据
  221. //DateTime ispre = Convert.ToDateTime(entity.CurLoginTime).AddDays(-1);
  222. //if(ispre.Day == Convert.ToDateTime(entity.PreLoginTime).Day)
  223. //{
  224. // CeErpUserInfo userInfo = CeErpUserInfo.Get(entity.ID);
  225. // userInfo.DayOrderPer = 0;
  226. // userInfo.DayOrderReceive = 0;
  227. // userInfo.OnDuty = 0;
  228. // userInfo.Update();
  229. //}
  230. entity.Update();
  231. Log_Login.Save(account, ip, false, "登录成功!");
  232. string isFromClient = GetPostString("isFromClient");//C端登录
  233. if ("1" == isFromClient)
  234. {
  235. returnSuccessMsg(entity.ID.ToString());
  236. return;
  237. }
  238. HttpCookie cookie = new HttpCookie(webConfig.CookieName);
  239. cookie.Values.Clear();
  240. cookie.Values.Add("user", HttpUtility.UrlEncode(account));
  241. cookie.Values.Add("ticket", entity.Ticket);
  242. //cookie.Values.Add("mullogin", entity.IsMultiLogin.ToString());
  243. cookie.Path = "/";
  244. //cookie.Domain = SiteDomain;
  245. //if (chkWeek.Checked) cookie.Expires = DateTime.Now.AddDays(14);
  246. cookie.Expires = DateTime.Now.AddHours(18);
  247. HttpContext.Current.Response.Cookies.Add(cookie);
  248. erpRedis.RedisHelper.StringSet("dl_" + account, entity.Ticket, new TimeSpan(18, 0, 0));
  249. //erpRedis.RedisHelper.StringSet(user, con.Session.SessionID);
  250. //erpRedis.RedisHelper.StringSet("PUB_" + user, con.Session.SessionID);
  251. con.Session["vCode"] = null;
  252. con.Session["lerr"] = null;
  253. con.Session["userId"] = entity.ID;
  254. //WebUser.SetUser(account, entity.Ticket);
  255. con.Session["_t"] = DateTime.Now.ToString("yyyyMMddHHmmssffff");
  256. if (isSimplePwd(pwd))
  257. con.Session["isSimplePwd"] = 1;
  258. else
  259. con.Session["isSimplePwd"] = null;
  260. returnSuccessMsg("登录成功!");
  261. return;
  262. }
  263. private bool isSimplePwd(string pwd)
  264. {
  265. Match result = Regex.Match(pwd, "(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{6,20}");
  266. if (result.Length > 0)
  267. {
  268. return false;
  269. }
  270. return true;
  271. }
  272. private bool isLoginOut()
  273. {
  274. ////人员是否已登录
  275. //if(con.Session["userId"] == null || con.Session["userId"].ToString() == "")
  276. // return true;
  277. //传过来的Ticket是否有效
  278. HttpCookie lcCookie = HttpContext.Current.Request.Cookies[webConfig.CookieName];
  279. if (lcCookie == null)
  280. {
  281. return true;
  282. }
  283. string name = lcCookie.Values["user"];
  284. string ticket = lcCookie.Values["ticket"];
  285. if (name == null || ticket == null || name == "" || ticket == "")
  286. {
  287. return true;
  288. }
  289. name = HttpUtility.UrlDecode(name);
  290. string dl_ticket = erpRedis.RedisHelper.StringGet("dl_" + name);
  291. if (ticket != dl_ticket)
  292. {
  293. return true;
  294. }
  295. return false;
  296. }
  297. public void loginout()
  298. {
  299. HttpCookie cookie = HttpContext.Current.Request.Cookies[webConfig.CookieName];
  300. string name = cookie.Values["user"];
  301. if (name != null && name != null)
  302. {
  303. name = HttpUtility.UrlDecode(name);
  304. bool b = erpRedis.RedisHelper.KeyDelete("dl_" + name);
  305. WebUser.RemoveUserCache(name);
  306. WebUser.RemovePermissionCache(name);
  307. }
  308. cookie.Expires = DateTime.Now.AddDays(-1d);
  309. HttpContext.Current.Response.Cookies.Add(cookie);
  310. cookie.Values.Clear();
  311. HttpContext.Current.Session["WEBUSER"] = null;
  312. con.Session["userId"] = null;
  313. con.Session["_t"] = null;
  314. returnSuccessMsg("退出成功!");
  315. }
  316. public static FrequencyControler DoFrequency = new FrequencyControler("xinyue", 10, 3);
  317. //定义访问控制器允许10秒内3次请求
  318. public void get_syslog()
  319. {
  320. DataStruct dStruct = GetPostStruct();
  321. dStruct.Order = "id desc";
  322. DataTable dt = WebCache.GetData("x_log", dStruct);
  323. string data = Utils.Serialization.JsonString.DataTable2MiniAjaxJson(dt);
  324. data = data.Replace("\\", "\\\\");
  325. writeGridJson(dStruct.TotalCount, data);
  326. }
  327. public void clear_syslog()
  328. {
  329. string sql = "truncate table x_log";
  330. DbHelper.DbConn.ExecuteNonQuery(sql);
  331. returnSuccessMsg("清空完成");
  332. }
  333. public void get_erp_ipWhitelist()
  334. {
  335. DataStruct dStruct = GetPostStruct();
  336. dStruct.Order = "id desc";
  337. List<string> lw = new List<string>();
  338. string ip = GetPostString("ip");
  339. if (ip.Length > 0) lw.Add(string.Format("ip_white_list like '%{0}%'", ip));
  340. string remark = GetPostString("remark");
  341. if (remark.Length > 0) lw.Add(string.Format("remark like '%{0}%'", remark));
  342. dStruct.MainWhere = string.Join(" and ", lw.ToArray());
  343. DataTable dt = WebCache.GetData("SiteInfo", dStruct);
  344. writeGridDataTableJson(dStruct.TotalCount, dt);
  345. }
  346. public void get_login_list()
  347. {
  348. DataStruct dStruct = GetPostStruct();
  349. dStruct.Order = "login_time desc";
  350. List<string> lw = new List<string>();
  351. lw.Add(string.Format("user_name != '{0}'", "xfd666"));
  352. string ip = GetPostString("ip");
  353. if (ip.Length > 0) lw.Add(string.Format("login_ip like '%{0}%'", ip));
  354. string name = GetPostString("uName");
  355. if (name.Length > 0) lw.Add(string.Format("user_name like '%{0}%'", name));
  356. string sTime = GetPostString("sTime");
  357. string eTime = GetPostString("eTime");
  358. if (sTime.Length > 0)
  359. {
  360. string dw = GetDateMinuteWhere("login_time", sTime, eTime);
  361. if (dw.Length > 0) lw.Add(dw);
  362. }
  363. dStruct.MainWhere = string.Join(" and ", lw.ToArray());
  364. DataTable dt = WebCache.GetData("Log_Login", dStruct);
  365. writeGridDataTableJson(dStruct.TotalCount, dt);
  366. }
  367. public void get_supplierlog()
  368. {
  369. DataStruct dStruct = GetPostStruct();
  370. dStruct.Order = "id desc";
  371. DataTable dt = WebCache.GetData("SupplierLog", dStruct);
  372. string data = Utils.Serialization.JsonString.DataTable2MiniAjaxJson(dt);
  373. data = data.Replace("\\", "\\\\");
  374. writeGridJson(dStruct.TotalCount, data);
  375. }
  376. }
  377. }