LoginByAD1.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Data;
  6. using Curtain.DataAccess;
  7. using DK.XuWei.WebMes;
  8. /// <summary>
  9. /// Login 的摘要说明
  10. /// </summary>
  11. namespace DK.XuWei.WebMes
  12. {
  13. public static partial class dkmes
  14. {
  15. public static JsonResult LoginByAD1(string userCodeAD)
  16. {
  17. JsonResult result = new JsonResult();
  18. using (IDataAccess conn = DataAccess.Create())
  19. {
  20. string userId = "";
  21. DateTime today = DateTime.Now;
  22. string guid = Guid.NewGuid().ToString();
  23. //开启事务
  24. conn.BeginTransaction();
  25. #region 读取用户信息
  26. DataTable userTable = conn.ExecuteDatatable(@"
  27. SELECT
  28. u.ACCOUNTID,
  29. u.ACCOUNTCODE,
  30. u.USERID,
  31. u.USERCODE,
  32. u.USERNAME,
  33. u.PASSWORD,
  34. u.ORGANIZATIONID,
  35. u.ISWORKER,
  36. u.LIMITSTARTTIME,
  37. u.LIMITENDTIME
  38. FROM
  39. TP_MST_USER u
  40. WHERE
  41. u.VALUEFLAG = '1'
  42. AND u.ad_user_code = @ad_user_code@
  43. ",
  44. new CDAParameter("ad_user_code", userCodeAD)
  45. );
  46. #endregion
  47. //验证用户密码
  48. if (userTable.Rows.Count == 1)
  49. {
  50. userId = userTable.Rows[0]["USERID"].ToString();
  51. result.success = true;
  52. result.message = "用户登录成功!";
  53. //if (userTable.Rows[0]["PASSWORD"].ToString() == userPassword)
  54. //{
  55. //}
  56. //else
  57. //{
  58. // //更新密码错误次数
  59. // conn.ExecuteNonQuery(@"
  60. // UPDATE
  61. // TP_MST_USERLOGIN
  62. // SET
  63. // MISSTIMES = MISSTIMES + 1
  64. // WHERE
  65. // USERID = @USERID@
  66. // ",
  67. // new CDAParameter("USERID", userId)
  68. // );
  69. // //读取错误次数
  70. // string missTimes = conn.ExecuteScalar(@"
  71. // SELECT
  72. // MISSTIMES
  73. // FROM
  74. // TP_MST_USERLOGIN
  75. // WHERE
  76. // USERID = @USERID@
  77. // ",
  78. // new CDAParameter("USERID", userId)
  79. // ).ToString();
  80. // result.success = false;
  81. // result.message = "用户名或密码错误!";
  82. // result.message += "登录失败次数:" + missTimes + ",";
  83. // result.message += "连续登录失败 " + LoginSetting.MissTimes.ToString() + " 次,";
  84. // result.message += "您的账号将被锁定 " + LoginSetting.LockHour.ToString() + " 小时!";
  85. // //错误次数上限锁定操作
  86. // //更新锁定标识
  87. // int isLock = conn.ExecuteNonQuery(@"
  88. // UPDATE
  89. // TP_MST_USERLOGIN
  90. // SET
  91. // ISLOCKED = '1',
  92. // LOCKTIME = @LOCKTIME@
  93. // WHERE
  94. // USERID = @USERID@
  95. // AND MISSTIMES >= @MISSTIMES@
  96. // ",
  97. // new CDAParameter("LOCKTIME", today),
  98. // new CDAParameter("USERID", userId),
  99. // new CDAParameter("MISSTIMES", LoginSetting.MissTimes)
  100. // );
  101. // //写入锁定时间
  102. // if(isLock>0)
  103. // {
  104. // conn.ExecuteNonQuery(@"
  105. // UPDATE
  106. // TP_MST_USER
  107. // SET
  108. // LIMITSTARTTIME = @LIMITSTARTTIME@,
  109. // LIMITENDTIME = @LIMITENDTIME@
  110. // WHERE
  111. // USERID = @USERID@
  112. // ",
  113. // new CDAParameter("LIMITSTARTTIME", today),
  114. // new CDAParameter("LIMITENDTIME", today.AddHours(LoginSetting.LockHour)),
  115. // new CDAParameter("USERID", userId)
  116. // );
  117. // result.success = false;
  118. // result.message = "用户账户已被锁定!";
  119. // result.message += " 锁定时间:" + today.ToString() + " 至 " + today.AddHours(LoginSetting.LockHour);
  120. // }
  121. //}
  122. }
  123. else
  124. {
  125. result.success = false;
  126. result.message = "用户名错误!";
  127. }
  128. //验证是否被锁定
  129. //if (result.success == true)
  130. //{
  131. // DataTable lockTable = conn.ExecuteDatatable(@"
  132. // SELECT
  133. // u.LIMITSTARTTIME,
  134. // u.LIMITENDTIME
  135. // FROM
  136. // TP_MST_USER u
  137. // LEFT JOIN TP_MST_USERLOGIN l ON u.USERID = l.USERID
  138. // WHERE
  139. // l.USERID = @USERID@
  140. // AND l.ISLOCKED = '1'
  141. // AND @LIMITSTARTTIME@ > u.LIMITSTARTTIME
  142. // AND @LIMITENDTIME@ < u.LIMITENDTIME
  143. // ",
  144. // new CDAParameter("USERID", userId),
  145. // new CDAParameter("LIMITSTARTTIME", today),
  146. // new CDAParameter("LIMITENDTIME", today)
  147. // );
  148. // if (lockTable.Rows.Count == 0)
  149. // {
  150. // result.success = true;
  151. // result.message = "用户登录成功!";
  152. // }
  153. // else
  154. // {
  155. // result.success = false;
  156. // result.message = "用户账户已被锁定!";
  157. // result.message += " 锁定时间:" + lockTable.Rows[0]["LIMITSTARTTIME"].ToString() + " 至 " + lockTable.Rows[0]["LIMITENDTIME"].ToString();
  158. // }
  159. //}
  160. //更新登录信息并记录Session
  161. if (result.success == true)
  162. {
  163. //重置密码错误次数 更新最后登录时间 更新SessionKey 更新登录状态
  164. conn.ExecuteNonQuery(@"
  165. UPDATE
  166. TP_MST_USERLOGIN
  167. SET
  168. ISLOCKED = '0',
  169. MISSTIMES = 0,
  170. LASTLOGINTIME = @LASTLOGINTIME@,
  171. WEBISLOGIN = '1',
  172. WEBSESSIONKEY = @WEBSESSIONKEY@
  173. WHERE
  174. USERID = @USERID@
  175. ",
  176. new CDAParameter("LASTLOGINTIME", today),
  177. new CDAParameter("WEBSESSIONKEY", guid),
  178. new CDAParameter("USERID", userId)
  179. );
  180. //记录Session
  181. HttpContext.Current.Session["accountId"] = userTable.Rows[0]["ACCOUNTID"].ToString();
  182. HttpContext.Current.Session["accountCode"] = userTable.Rows[0]["ACCOUNTCODE"].ToString();
  183. HttpContext.Current.Session["userId"] = userTable.Rows[0]["USERID"].ToString();
  184. HttpContext.Current.Session["userCode"] = userTable.Rows[0]["USERCODE"].ToString();
  185. HttpContext.Current.Session["userPassword"] = userTable.Rows[0]["PASSWORD"].ToString();
  186. HttpContext.Current.Session["webSessionKey"] = guid;
  187. //如果是成检 必须更新 sessionKey 会顶掉PC或PDA登录的相同用户
  188. //if (productCheck)
  189. //{
  190. // conn.ExecuteNonQuery(@"
  191. // UPDATE
  192. // TP_MST_USERLOGIN
  193. // SET
  194. // SESSIONKEY = @SESSIONKEY@
  195. // WHERE
  196. // USERID = @USERID@
  197. // ",
  198. // new CDAParameter("SESSIONKEY", guid),
  199. // new CDAParameter("USERID", userId)
  200. // );
  201. // HttpContext.Current.Session["sessionKey"] = guid;
  202. //}
  203. }
  204. //提交数据
  205. conn.Commit();
  206. return result;
  207. }
  208. }
  209. }
  210. }