LoginExcutor.cs 6.0 KB

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