郑许地铁

DefaultController.cs 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. using CallCenterApi.Interface.Controllers.Login;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Net.NetworkInformation;
  8. using System.Runtime.InteropServices;
  9. using System.Text;
  10. using System.Web;
  11. using System.Web.Mvc;
  12. using ZXDT.CallCenter.Model;
  13. using ZXDT.CallCenter.MVCWeb.Models;
  14. using ZXDT.Common;
  15. namespace ZXDT.CallCenter.MVCWeb.Controllers
  16. {
  17. public class DefaultController : Controller
  18. {
  19. BLL.T_Sys_UserAccount busUserInfo = new BLL.T_Sys_UserAccount();
  20. public ActionResult Login()
  21. {
  22. T_Sys_UserAccount userInfo = new T_Sys_UserAccount();
  23. HttpCookie cookies = Request.Cookies["platform"];
  24. //判断是否有cookie值,有的话就读取出来
  25. if (cookies != null && cookies.HasKeys)
  26. {
  27. userInfo.F_UserName = cookies["Name"];
  28. }
  29. userInfo.ExtensionCode = ConfigurationManager.AppSettings["sso"].ToMyString();
  30. // userInfo.F_UserName = SystemInfo.GetCpuPerformancevalue();
  31. return View(userInfo);
  32. }
  33. [AcceptVerbs(HttpVerbs.Post)]
  34. public string LoginIn(string username, string extensionphone, string password,string logintime)
  35. {
  36. string res = "未知错误,请重新登录";
  37. DataTable dt = new DataTable();
  38. try
  39. {
  40. #region 密码解密
  41. string st = DateTime.Parse(logintime).ToString("HHmmss");
  42. var otherController = DependencyResolver.Current.GetService<AES256Controller>();
  43. string pw = "";
  44. try
  45. {
  46. pw = otherController.Decrypt(password.Trim(), st);
  47. }
  48. catch (Exception e)
  49. {
  50. res = "错误:" + e.Message;
  51. }
  52. #endregion
  53. dt = busUserInfo.GetList(username, pw).Tables[0];
  54. if (dt != null)
  55. {
  56. if (dt.Rows.Count > 0)
  57. {
  58. if (dt.Rows[0]["F_UserCode"].ToString() == dt.Rows[0]["F_PassWord"].ToString())
  59. {
  60. res = "firstlogin" + dt.Rows[0]["F_UserId"].ToString();
  61. }
  62. else
  63. {
  64. //记住用户名每3个月更换一次
  65. HttpCookie cookie = new HttpCookie("platform");
  66. cookie.Values.Add("Name", username);
  67. cookie.Values.Add("islocal", "1");
  68. cookie.Expires = System.DateTime.Now.AddMonths(3);
  69. Response.Cookies.Add(cookie);
  70. if (!string.IsNullOrEmpty(extensionphone))
  71. {
  72. }
  73. else if (string.IsNullOrEmpty(dt.Rows[0]["F_ExtensionNumber"].ToMyString()))
  74. {
  75. extensionphone = dt.Rows[0]["ExtensionCode"].ToMyString();
  76. }
  77. else
  78. {
  79. extensionphone = dt.Rows[0]["F_ExtensionNumber"].ToMyString();
  80. }
  81. int tempExtensionphone = GetNetworkAdpaterID();
  82. if (tempExtensionphone > 0)
  83. {
  84. extensionphone = tempExtensionphone.ToMyString();
  85. }
  86. LogHelper.Default.WriteInfo("tsKey:" + extensionphone);
  87. Dictionary<string, string> Dic = new Dictionary<string, string>();
  88. Dic.Add("F_UserID", dt.Rows[0]["F_UserId"].ToString());
  89. Dic.Add("F_UserCode", dt.Rows[0]["F_UserCode"].ToString());
  90. Dic.Add("F_DeptId", dt.Rows[0]["F_DeptId"].ToString());
  91. Dic.Add("F_UserName", HttpUtility.UrlEncode(dt.Rows[0]["F_UserName"].ToString(), Encoding.UTF8));
  92. Dic.Add("F_Telephone", dt.Rows[0]["F_Telephone"].ToString());
  93. Dic.Add("F_RoleID", dt.Rows[0]["F_RoleID"].ToString());
  94. Dic.Add("F_SeatFlag", dt.Rows[0]["F_SeatFlag"].ToString());
  95. Dic.Add("F_PId", dt.Rows[0]["F_PId"].ToString());
  96. if (dt.Rows[0]["F_GroupId"].ToString() != null)
  97. {
  98. Dic.Add("F_GroupId", dt.Rows[0]["F_GroupId"].ToString());
  99. }
  100. if (dt.Rows[0]["F_SeatRight"].ToString() != null)
  101. {
  102. Dic.Add("F_SeatRight", dt.Rows[0]["F_SeatRight"].ToString());
  103. }
  104. Dic.Add("F_ExtensionNumber", extensionphone);
  105. Dic.Add("F_WorkNumber", dt.Rows[0]["F_WorkNumber"].ToString());
  106. CookieUtil.SetCookie("BaseCallCenter_T_User", Dic);
  107. //写入登录日志
  108. new BLL.T_Sys_LoginLogs().Add(new Model.T_Sys_LoginLogs()
  109. {
  110. F_LoginName = dt.Rows[0]["F_UserName"].ToString(),
  111. F_LoginId = Convert.ToInt32(dt.Rows[0]["F_UserId"].ToString()),
  112. F_Result = "登录成功",
  113. F_LoginIP = Common.DTRequest.GetIP(),
  114. F_Hostname = Common.DTRequest.GetIP(),
  115. F_LoginDate = DateTime.Now,
  116. F_Remark = "",
  117. F_State = 0
  118. });
  119. Common.CommonRequest.dtUserId = Convert.ToInt32(dt.Rows[0]["F_UserId"].ToString());
  120. Common.CommonRequest.dtUserCode = dt.Rows[0]["F_UserCode"].ToString();//Convert.ToInt32(dt.Rows[0]["F_UserId"].ToString());
  121. res = "success";
  122. //绑定登录用户分机号
  123. BLL.T_Sys_UserAccount bll = new BLL.T_Sys_UserAccount();
  124. bll.UpdateWXId("ExtensionCode",extensionphone.ToInt32(), Convert.ToInt32(dt.Rows[0]["F_UserId"].ToString())); }
  125. }
  126. else
  127. {
  128. //写入登录日志
  129. DataTable dt1 = busUserInfo.GetList("F_UserCode='" + username + "'").Tables[0];
  130. if (dt1.Rows.Count > 0)
  131. {
  132. int rr = new BLL.T_Sys_LoginLogs().Add(new Model.T_Sys_LoginLogs()
  133. {
  134. F_LoginName = dt1.Rows[0]["F_UserName"].ToString(),
  135. F_LoginId = Convert.ToInt32(dt1.Rows[0]["F_UserId"].ToString()),
  136. F_Result = "登录失败:帐号-" + username + ";分机号-" + extensionphone,
  137. F_LoginIP = Common.DTRequest.GetIP(),
  138. F_Hostname = Common.DTRequest.GetIP(),
  139. F_LoginDate = DateTime.Now,
  140. F_Remark = "",
  141. F_State = 0
  142. });
  143. }
  144. else
  145. {
  146. int rr = new BLL.T_Sys_LoginLogs().Add(new Model.T_Sys_LoginLogs()
  147. {
  148. F_LoginName = username,
  149. F_LoginId = -1,
  150. F_Result = "登录失败:帐号-" + username + ";分机号-" + extensionphone,
  151. F_LoginIP = Common.DTRequest.GetIP(),
  152. F_Hostname = Common.DTRequest.GetIP(),
  153. F_LoginDate = DateTime.Now,
  154. F_Remark = "",
  155. F_State = 0
  156. });
  157. }
  158. res = "账号或密码错误,请重新登录";
  159. }
  160. }
  161. else
  162. {
  163. res = "账号或密码错误,请重新登录";
  164. }
  165. }
  166. catch (Exception ex)
  167. {
  168. res = "错误:" + ex.ToString();
  169. SysLog.WriteLog(ex);
  170. }
  171. finally
  172. {
  173. dt.Clear();
  174. dt.Dispose();
  175. }
  176. return res;
  177. }
  178. //退出系统
  179. [AcceptVerbs(HttpVerbs.Post)]
  180. public string LoginOut(string username)
  181. {
  182. string res = "未知错误,请重新登录";
  183. DataTable dt = new DataTable();
  184. try {
  185. string userid = Request.Cookies["BaseCallCenter_T_User"]["F_UserId"];
  186. dt = busUserInfo.GetList("F_UserId='" + userid + "'").Tables[0];
  187. //写入登录日志
  188. new BLL.T_Sys_LoginLogs().Add(new Model.T_Sys_LoginLogs()
  189. {
  190. F_LoginName = dt.Rows[0]["F_UserName"].ToString(),
  191. F_LoginId = Convert.ToInt32(dt.Rows[0]["F_UserId"].ToString()),
  192. F_Result = "退出系统成功",
  193. F_LoginIP = Common.DTRequest.GetIP(),
  194. F_Hostname = Common.DTRequest.GetIP(),
  195. F_LoginDate = DateTime.Now,
  196. F_Remark = "",
  197. F_State = 0
  198. });
  199. res = "success";
  200. //绑定登录用户分机号
  201. BLL.T_Sys_UserAccount bll = new BLL.T_Sys_UserAccount();
  202. bll.UpdateWXId("ExtensionCode", 0, Convert.ToInt32(dt.Rows[0]["F_UserId"].ToString()));
  203. }
  204. catch(Exception){ }
  205. return res;
  206. }
  207. public ActionResult SSO()
  208. {
  209. bool res = false; //标记是否登录
  210. string LoginName=Request.Headers.Get("OAM_REMOTE_USER");
  211. LogHelper.Default.WriteInfo("LoginName:" + LoginName.ToMyString());
  212. if (!string.IsNullOrEmpty(LoginName))
  213. {
  214. if (!"Anonymous".Equals(LoginName))
  215. {
  216. res = true;
  217. TimeSpan ts = DateTime.Now - new DateTime(1999, 1, 1, 0, 0, 0, 0);
  218. int ss = Convert.ToInt32(ts.TotalSeconds);
  219. string key = DESEncrypt.Encrypt(ss.ToString(), "SSO" + LoginName);
  220. return new RedirectResult(string.Format("{2}/default/SSOShow?userid={0}&key={1}", LoginName, key, ConfigurationManager.AppSettings["domain"]));
  221. }
  222. }
  223. return View();
  224. }
  225. public ActionResult SSOShow()
  226. {
  227. ReturnData rd = new ReturnData { code = -1, msg = "默认错误",data= ConfigurationManager.AppSettings["sso"] };
  228. string userid = Request.QueryString["userid"].ToMyString();
  229. string key = Request.QueryString["key"].ToMyString();
  230. if (userid != "" && key != "")
  231. {
  232. TimeSpan ts = DateTime.Now - new DateTime(1999, 1, 1, 0, 0, 0, 0);
  233. int tsInt = Convert.ToInt32(ts.TotalSeconds);
  234. int tsKey = DESEncrypt.Decrypt(key, "SSO" + userid).ToInt32();
  235. //LogHelper.Default.WriteInfo("tsInt:" + tsInt.ToMyString());
  236. //LogHelper.Default.WriteInfo("tsKey:" + tsKey.ToMyString());
  237. if (tsInt - tsKey < 5)
  238. {
  239. DataTable dt = new DataTable();
  240. try
  241. {
  242. DataSet ds = busUserInfo.GetList(" F_UserCode=" + userid);
  243. if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
  244. {
  245. dt = ds.Tables[0];
  246. rd.code = 0;
  247. rd.msg = "成功";
  248. //记住用户名每3个月更换一次
  249. HttpCookie cookie = new HttpCookie("platform");
  250. cookie.Values.Add("Name", userid);
  251. cookie.Values.Add("islocal", "0");
  252. cookie.Expires = System.DateTime.Now.AddMonths(3);
  253. Response.Cookies.Add(cookie);
  254. string extensionphone = "";
  255. if (!string.IsNullOrEmpty(extensionphone))
  256. {
  257. }
  258. else if (string.IsNullOrEmpty(dt.Rows[0]["F_ExtensionNumber"].ToMyString()))
  259. {
  260. extensionphone = dt.Rows[0]["ExtensionCode"].ToMyString();
  261. }
  262. else
  263. {
  264. extensionphone = dt.Rows[0]["F_ExtensionNumber"].ToMyString();
  265. }
  266. int tempExtensionphone = GetNetworkAdpaterID();
  267. if (tempExtensionphone > 0)
  268. {
  269. extensionphone = tempExtensionphone.ToMyString();
  270. }
  271. Dictionary<string, string> Dic = new Dictionary<string, string>();
  272. Dic.Add("F_UserID", dt.Rows[0]["F_UserId"].ToMyString());
  273. Dic.Add("F_UserCode", dt.Rows[0]["F_UserCode"].ToMyString());
  274. Dic.Add("F_DeptId", dt.Rows[0]["F_DeptId"].ToMyString());
  275. Dic.Add("F_UserName", HttpUtility.UrlEncode(dt.Rows[0]["F_UserName"].ToMyString(), Encoding.UTF8));
  276. Dic.Add("F_Telephone", dt.Rows[0]["F_Telephone"].ToMyString());
  277. Dic.Add("F_RoleID", dt.Rows[0]["F_RoleID"].ToMyString());
  278. Dic.Add("F_SeatFlag", dt.Rows[0]["F_SeatFlag"].ToMyString());
  279. Dic.Add("F_PId", dt.Rows[0]["F_PId"].ToMyString());
  280. Dic.Add("F_GroupId", dt.Rows[0]["F_GroupId"].ToMyString());
  281. Dic.Add("F_SeatRight", dt.Rows[0]["F_SeatRight"].ToMyString());
  282. Dic.Add("F_ExtensionNumber", extensionphone);
  283. Dic.Add("F_WorkNumber", dt.Rows[0]["F_WorkNumber"].ToMyString());
  284. CookieUtil.SetCookie("BaseCallCenter_T_User", Dic);
  285. //写入登录日志
  286. new BLL.T_Sys_LoginLogs().Add(new Model.T_Sys_LoginLogs()
  287. {
  288. F_LoginName = dt.Rows[0]["F_UserName"].ToString(),
  289. F_LoginId = dt.Rows[0]["F_UserId"].ToInt32(),
  290. F_Result = "单点登录成功",
  291. F_LoginIP = Common.DTRequest.GetIP(),
  292. F_Hostname = Common.DTRequest.GetIP(),
  293. F_LoginDate = DateTime.Now,
  294. F_Remark = "",
  295. F_State = 0
  296. });
  297. Common.CommonRequest.dtUserId = Convert.ToInt32(dt.Rows[0]["F_UserId"].ToString());
  298. Common.CommonRequest.dtUserCode = dt.Rows[0]["F_UserCode"].ToString();
  299. //绑定登录用户分机号
  300. BLL.T_Sys_UserAccount bll = new BLL.T_Sys_UserAccount();
  301. bll.UpdateWXId("ExtensionCode", extensionphone.ToInt32(), dt.Rows[0]["F_UserId"].ToInt32());
  302. }
  303. else
  304. {
  305. rd.msg = "没有权限";
  306. }
  307. }
  308. catch (Exception ex)
  309. {
  310. rd.msg = "error";
  311. LogHelper.Default.WriteError(ex.Message, ex);
  312. }
  313. finally
  314. {
  315. dt.Clear();
  316. dt.Dispose();
  317. }
  318. }
  319. else
  320. {
  321. rd.msg = "非法路径";
  322. }
  323. }
  324. else
  325. {
  326. rd.msg = "没有登录";
  327. }
  328. if (rd.code == 0)
  329. {
  330. return RedirectToAction("Index", "Main");
  331. }
  332. else
  333. return View(rd);
  334. }
  335. /// <summary>
  336. /// 获取网卡ID代码
  337. /// </summary>
  338. /// <returns></returns>
  339. public static int GetNetworkAdpaterID()
  340. {
  341. int ret = 0;
  342. //NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
  343. //if (nics != null && nics.Length > 0)
  344. //{
  345. // foreach (NetworkInterface nic in nics)
  346. // { string mac = nic.GetPhysicalAddress().ToString();
  347. // LogHelper.Default.WriteInfo("MAC:" + mac);
  348. // if (string.IsNullOrEmpty(mac))
  349. // continue;
  350. // ret = new BLL.T_Mac().GetNum(mac);
  351. // if (ret > 0)
  352. // {
  353. // LogHelper.Default.WriteInfo("MAC1:" + mac);
  354. // break;
  355. // }
  356. // }
  357. //}
  358. string mac = GetClientMAC();
  359. ret = new BLL.T_Mac().GetNum(mac);
  360. return ret;
  361. }
  362. [DllImport("Iphlpapi.dll")]
  363. private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
  364. [DllImport("Ws2_32.dll")]
  365. private static extern Int32 inet_addr(string ip);
  366. private static string GetClientMAC()
  367. {
  368. string mac_dest = string.Empty;
  369. // 在此处放置用户代码以初始化页面
  370. try
  371. {
  372. string userip = System.Web.HttpContext.Current.Request.UserHostAddress;
  373. string strClientIP = System.Web.HttpContext.Current.Request.UserHostAddress.ToString().Trim();
  374. Int32 ldest = inet_addr(strClientIP); //目的地的ip
  375. Int32 lhost = inet_addr(""); //本地服务器的ip
  376. Int64 macinfo = new Int64();
  377. Int32 len = 6;
  378. int res = SendARP(ldest, 0, ref macinfo, ref len);
  379. string mac_src = macinfo.ToString("X");
  380. while (mac_src.Length < 12)
  381. {
  382. mac_src = mac_src.Insert(0, "0");
  383. }
  384. for (int i = 0; i < 11; i++)
  385. {
  386. if (0 == (i % 2))
  387. {
  388. if (i == 10)
  389. {
  390. mac_dest = mac_dest.Insert(0, mac_src.Substring(i, 2));
  391. }
  392. else
  393. {
  394. mac_dest = "-" + mac_dest.Insert(0, mac_src.Substring(i, 2));
  395. }
  396. }
  397. }
  398. }
  399. catch (Exception ex)
  400. {
  401. throw ex;
  402. }
  403. return mac_dest;
  404. }
  405. }
  406. }