LoginExcutor.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Linq;
  6. using System.Text;
  7. using JLHHJSvr.BLL;
  8. using JLHHJSvr.Com;
  9. using JLHHJSvr.Com.Model;
  10. using JLHHJSvr.DBA.DBModle;
  11. using JLHHJSvr.LJException;
  12. using LJLib.DAL.SQL;
  13. using LJLib.Net.SPI.Server;
  14. using LJLib.SQLEX;
  15. using LJLib.Tools.DEncrypt;
  16. using LJLib.Tools.Encry;
  17. namespace JLHHJSvr.Excutor
  18. {
  19. internal sealed class LoginExcutor : ExcutorBase<LoginRequest, LoginResponse>
  20. {
  21. protected override void ExcuteInternal(LoginRequest request, object state, LoginResponse rslt)
  22. {
  23. if (string.IsNullOrEmpty(request.usercode))
  24. {
  25. rslt.ErrMsg = "用户名不能为空";
  26. return;
  27. }
  28. var remoteIP = string.Empty;
  29. var remoteInfo = state as IRemoteInfoContainer;
  30. var remoteEndPoint = remoteInfo?.RemoteInfo;
  31. if (!string.IsNullOrEmpty(remoteEndPoint))
  32. {
  33. var pos = remoteEndPoint.LastIndexOf(":");
  34. remoteIP = pos > 0 ? remoteEndPoint.Substring(0, pos).Trim() : remoteEndPoint;
  35. }
  36. //if (string.IsNullOrEmpty(request.psw))
  37. //{
  38. // rslt.ErrMsg = "密码不能为空";
  39. //}
  40. u_user_jlhprice stUser = new u_user_jlhprice();
  41. rslt.rsltFunids = new List<int>();
  42. using (var con = GlobalVar.ConnectionString.NewSqlConnection())
  43. using (var cmd = con.CreateCommand())
  44. {
  45. con.Open();
  46. try
  47. {
  48. if (DbSqlHelper.SelectOne(cmd, "u_user_jlhprice", "userid = @usercode",
  49. new Dictionary<string, object>() { { "@usercode", request.usercode } }, stUser,
  50. "userid, empid, username, usermode, psw, access_failed_count, last_failed_attempt_time,whiteIPs") != 1)
  51. {
  52. rslt.ErrMsg = "用户名不存在或密码错误";
  53. return;
  54. }
  55. // 判断是否lock
  56. if (stUser.isLocked)
  57. {
  58. throw new LJCommonException("登录连续错误5次,账号已锁定,请联系管理员解锁!");
  59. }
  60. if (!string.IsNullOrEmpty(stUser.whiteIPs) && !string.IsNullOrEmpty(remoteIP))
  61. {
  62. var ipSet = new HashSet<string>(stUser.whiteIPs.Split(',', ','), StringComparer.OrdinalIgnoreCase);
  63. if (!ipSet.Contains(remoteIP))
  64. {
  65. throw new LJCommonException($"{request.usercode}当前使用的IP[{remoteIP}]不在白名单里,不允许登录");
  66. }
  67. }
  68. psw_bczh3 pswhelper = new psw_bczh3();
  69. if (pswhelper.GetEntrypt(request.psw, 0, "123457851239866") != stUser.psw)
  70. {
  71. using (cmd.Transaction = con.BeginTransaction())
  72. {
  73. try
  74. {
  75. cmd.CommandText = @"UPDATE u_user_jlhprice SET access_failed_count = CASE
  76. WHEN last_failed_attempt_time < @failedDate THEN 1
  77. ELSE access_failed_count + 1
  78. END,
  79. last_failed_attempt_time = GETUTCDATE()
  80. WHERE u_user_jlhprice.empid = @empid";
  81. cmd.Parameters.Clear();
  82. cmd.Parameters.AddWithValue("@failedDate", DateTime.UtcNow.AddMonths(-1));
  83. cmd.Parameters.AddWithValue("@empid", stUser.empid);
  84. cmd.ExecuteNonQuery();
  85. cmd.Transaction.Commit();
  86. }
  87. catch (Exception e)
  88. {
  89. cmd.Transaction.Rollback();
  90. }
  91. }
  92. throw new LJCommonException($"密码错误,剩余尝试次数:{5 - stUser.access_failed_count + 1} 次(共 5 次)");
  93. }
  94. string token = Guid.NewGuid().ToString();
  95. rslt.token = token;
  96. rslt.username = stUser.username;
  97. rslt.usercode = stUser.userid;
  98. rslt.empid = stUser.empid;
  99. rslt.usermode = stUser.usermode;
  100. rslt.rsltFunids = UserHelper.FilterMyFunids(cmd, stUser.empid);
  101. var tokenData = new TokenData
  102. {
  103. empid = stUser.empid,
  104. usercode = stUser.userid,
  105. userid = stUser.empid,
  106. username = stUser.username,
  107. usermode = stUser.usermode
  108. };
  109. BllHelper.SetToken(token, tokenData);
  110. // 登录成功,清除错误次数
  111. using (cmd.Transaction = con.BeginTransaction())
  112. {
  113. try
  114. {
  115. UserHelper.UnLock(cmd, new List<int>() { stUser.empid });
  116. cmd.Transaction.Commit();
  117. }
  118. catch (Exception e)
  119. {
  120. cmd.Transaction.Rollback();
  121. }
  122. }
  123. }
  124. catch(LJCommonException ex)
  125. {
  126. rslt.ErrMsg = ex.Message;
  127. }
  128. }
  129. }
  130. }
  131. }