You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
7.6 KiB

  1. 
  2. using Newtonsoft.Json;
  3. using OT.COM.ArsenalDB;
  4. using OT.COM.LogisticsUtil;
  5. using OT.COM.SignalerMessage;
  6. using SoldierData;
  7. using SoldierData.syserp;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Data;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Text;
  14. using Newtonsoft.Json.Linq;
  15. using SoldierDataEntity;
  16. using System.DirectoryServices;
  17. using System.Collections;
  18. using System.Net;
  19. namespace CounsellorBL
  20. {
  21. public partial class AuthorityService : DBService
  22. {
  23. public const string MODE = "mode";
  24. protected CResponseMessage loginBase(CRequestMessage i_crm, otb_user i_u)
  25. {
  26. CResponseMessage crm = null;
  27. string sMsg = null;
  28. do
  29. {
  30. if (null == i_u)
  31. {
  32. sMsg = BaseExceptionWord.ex000001; //請輸入正確的帳號和密碼或該帳號無效
  33. break;
  34. }
  35. // Add row to session
  36. string sGUID = Guid.NewGuid().ToString();
  37. otb_session s = new otb_session();
  38. //s.user_name = i_u.user_name;
  39. s.create_user_guid = s.modify_user_guid = i_u.guid;
  40. //s.login_ip = i_crm.ClientIP;
  41. s.guid = sGUID;
  42. Command cAddSession = Command.SetupInsertCmd(GetMasterDBTableInfo(typeof(otb_session)), s);
  43. Command cDelSession = Command.SetupDeleteCmd(GetMasterDBTableInfo(typeof(otb_session)),
  44. new WhereNode(otb_session.CN_MODIFY_DATE, WhereNode.EColumnOperation.EOT_LT, typeof(otb_session), DateTime.Now.AddHours(-1)));
  45. if (0 >= this.adbm.RunEditCmds(new List<Command>() { cDelSession, cAddSession }))
  46. {
  47. sMsg = BaseExceptionWord.ex000002;//SESSION 新增失敗
  48. if (true == new Util().GetSettingBoolean("EnableDebug")) //ConfigurationManager.AppSettings <compilation debug="true"...
  49. {
  50. //sMsg += string.Format("({0})", cAddSession.LastErrorMsg);
  51. }
  52. break;
  53. }
  54. // Get GUID
  55. //decimal dFidLastInsert = (decimal)cAddSession.LastInsertIdentity;
  56. //int nFidLastInsert = Convert.ToInt32(dFidLastInsert);
  57. /*
  58. otb_session sFethch = new otb_session();
  59. sFethch.SetDirty(otb_session.CN_F_S_GUID);
  60. WhereNode wn = new WhereNode(otb_session.CN_F_S_GUID, WhereNode.EColumnOperation.EOT_EQ, sGUID);
  61. Command cWhereLastInsert = Command.SetupSelectCmd(GetMasterDBTableInfo(typeof(otb_session)), sFethch, wn);
  62. QueryDataTable qdr2 = this.adbm.RunQuery(cWhereLastInsert);
  63. DataTable dt2 = qdr2.DATA;*/
  64. crm = new CSuccessResponseMessage("LOGIN SUCCESS", i_crm);
  65. crm.DATA.Add(BLWording.TOKEN, sGUID);
  66. }
  67. while (false);
  68. if (null != sMsg)
  69. {
  70. crm = new CResponseMessage(i_crm) { RESULT = EResponseResult.RR_FALSE, MSG = sMsg };
  71. }
  72. return crm;
  73. }
  74. /// <summary>
  75. /// Login check
  76. /// </summary>
  77. /// <param name="i_crm"></param>
  78. /// <returns></returns>
  79. public CResponseMessage Login(CRequestMessage i_crm)
  80. {
  81. // IN
  82. CResponseMessage crm = null;
  83. string sMsg = null;
  84. string sEmprolyeeid = i_crm.DATA["user_id"].ToString();
  85. string sPassword = i_crm.DATA["password"].ToString();
  86. string sMethod = _fetchString(i_crm, "cmbloginmethod");
  87. //string sIp = _fetchString(i_crm, "Ip");
  88. string sIp = GetWebClientIp();
  89. // 加密
  90. //sMsg = EncryptMgr.Encrypt(sPassword, out sEnctPassword);
  91. do
  92. {
  93. QueryJson qj = new QueryJson();
  94. List<QueryJsonElement> lqje = new List<QueryJsonElement>();
  95. QueryJsonElement qjeA = new QueryJsonElement();
  96. qjeA.table = otb_user.TABLENAME;
  97. qjeA.tablealias = "a";
  98. qjeA.wherecols = new WhereNode(otb_user.CN_USER_ID, WhereNode.EColumnOperation.EOT_EQ, typeof(otb_user), sEmprolyeeid) { EQCaseSensitiveSupport = false };
  99. qjeA.ordercols = new List<Dictionary<string, string>>() { new Dictionary<string, string>() { { otb_user.CN_CREATE_DATE, "DESC" } } };
  100. qj.AddBlock(qjeA);
  101. Command cSelect = null;
  102. qj.MakeCommand(GetMasterDBTableInfo(typeof(otb_user)), out cSelect);
  103. otb_user uSelectItem = new otb_user();
  104. //uSelectItem.SetDirty(tb_user.F_N_ID);
  105. otb_user u = this.adbm.RunQuerySingleORM<otb_user>(cSelect);
  106. if (null == u)
  107. {
  108. sMsg = BaseExceptionWord.login001; //請輸入正確的帳號和密碼
  109. break;
  110. }
  111. // Expire
  112. if (u.active_flag != "Y")
  113. {
  114. sMsg = BaseExceptionWord.login002; //該帳號無效
  115. break;
  116. }
  117. crm = loginBase(i_crm, u);
  118. if (crm.RESULT == EResponseResult.RR_TRUE)
  119. {
  120. crm.DATA.Add(otb_user.CN_USER_ID, u.user_id);
  121. crm.DATA.Add(otb_user.CN_USER_NAME, u.user_name);
  122. crm.DATA.Add(BLWording.ENTITYS, new List<otb_user>() { u }); //這裡放entity登入帳號資料
  123. }
  124. }
  125. while (false);
  126. if (null != sMsg)
  127. {
  128. crm = new CResponseMessage(i_crm) { RESULT = EResponseResult.RR_FALSE, MSG = sMsg };
  129. }
  130. return crm;
  131. }
  132. #region 获取web客户端ip
  133. /// <summary>
  134. /// 获取web客户端ip
  135. /// </summary>
  136. /// <returns></returns>
  137. public static string GetWebClientIp()
  138. {
  139. string sUserIP = "";
  140. try
  141. {
  142. if (System.Web.HttpContext.Current == null
  143. || System.Web.HttpContext.Current.Request == null
  144. || System.Web.HttpContext.Current.Request.ServerVariables == null)
  145. return "";
  146. string CustomerIP = "";
  147. //CDN加速后取到的IP simone 090805
  148. CustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
  149. if (!string.IsNullOrEmpty(CustomerIP))
  150. {
  151. return CustomerIP;
  152. }
  153. CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
  154. if (!String.IsNullOrEmpty(CustomerIP))
  155. return CustomerIP;
  156. if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
  157. {
  158. CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
  159. if (CustomerIP == null)
  160. CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
  161. }
  162. else
  163. {
  164. CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
  165. }
  166. if (string.Compare(CustomerIP, "unknown", true) == 0)
  167. return System.Web.HttpContext.Current.Request.UserHostAddress;
  168. return CustomerIP;
  169. }
  170. catch { }
  171. return sUserIP;
  172. }
  173. #endregion
  174. }
  175. }