AccountController.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Transactions;
  5. using System.Web;
  6. using System.Web.Mvc;
  7. using System.Web.Security;
  8. using DotNetOpenAuth.AspNet;
  9. using Microsoft.Web.WebPages.OAuth;
  10. using WebMatrix.WebData;
  11. using Bowin.Common;
  12. using Bowin.Common.Utility;
  13. using EMIS.Utility.SSO;
  14. using EMIS.Utility;
  15. using EMIS.Web.Filters;
  16. using EMIS.Web.Models;
  17. using EMIS.Web.Controls;
  18. using EMIS.ViewModel;
  19. using EMIS.ViewModel.AccountManage;
  20. using EMIS.ViewModel.SystemView;
  21. using EMIS.CommonLogic.SystemServices;
  22. using EMIS.CommonLogic.EnrollManage.NewStudentManage;
  23. namespace EMIS.Web.Controllers
  24. {
  25. [Authorization]
  26. public class AccountController : Controller
  27. {
  28. public IUserServices UserServices { get; set; }
  29. public IMailVerifyServices MailVerifyServices { get; set; }
  30. public INewStudentEnterServices newStudentEnterServices { get; set; }
  31. /// <summary>
  32. ///
  33. /// </summary>
  34. /// <param name="returnUrl"></param>
  35. /// <returns></returns>
  36. [AllowAnonymous]
  37. public ActionResult Login(string returnUrl)
  38. {
  39. ViewBag.ReturnUrl = returnUrl;
  40. var model = this.GetUserCookies();
  41. if (model != null)
  42. {
  43. return View(model);
  44. }
  45. return View();
  46. }
  47. /// <summary>
  48. ///
  49. /// </summary>
  50. /// <param name="model"></param>
  51. /// <param name="returnUrl"></param>
  52. /// <returns></returns>
  53. [HttpPost]
  54. [AllowAnonymous]
  55. [ValidateAntiForgeryToken]
  56. public ActionResult Login(LogOnModel model, string returnUrl)
  57. {
  58. if (ModelState.IsValid)
  59. {
  60. try
  61. {
  62. if (model.VerifyCode != Session["code"] as string)
  63. {
  64. ModelState.AddModelError("", "请输入正确的验证码!");
  65. return View(model);
  66. }
  67. bool isLogin;
  68. try
  69. {
  70. isLogin = UserServices.Login(model.UserName, model.Password);
  71. }
  72. catch (Exception ex)
  73. {
  74. ModelState.AddModelError("", ex.Message);
  75. return View(model);
  76. }
  77. if (isLogin)
  78. {
  79. try
  80. {
  81. this.LoginSureccessful(model, model.UserName);
  82. var referrer = HttpContext.Request.UrlReferrer;
  83. if (referrer != null)
  84. {
  85. var referrerUrl = HttpContext.Request.UrlReferrer.GetQueryStringByPath("targetUrl");
  86. if (!string.IsNullOrEmpty(referrerUrl))
  87. {
  88. return Redirect(HttpContext.Server.UrlDecode(referrerUrl));
  89. }
  90. }
  91. return RedirectToAction("Index", "Home");
  92. }
  93. catch (Exception ex)
  94. {
  95. ModelState.AddModelError("", ex.Message);
  96. return View(model);
  97. }
  98. }
  99. else
  100. {
  101. ModelState.AddModelError("", "用户名或密码不正确请检查后重新输入!");
  102. this.RemoveUserCookies(model);
  103. return View(model);
  104. }
  105. }
  106. catch (Exception ex)
  107. {
  108. //this.RemoveUserCookies(model);
  109. //ModelState.AddModelError("", "提供的用户名或密码不正确。");
  110. throw ex;
  111. }
  112. }
  113. return View(model);
  114. }
  115. /// <summary>
  116. /// 河北工大
  117. /// </summary>
  118. /// <param name="returnUrl"></param>
  119. /// <returns></returns>
  120. [AllowAnonymous]
  121. public ActionResult HBGDLogin(string returnUrl)
  122. {
  123. ViewBag.ReturnUrl = returnUrl;
  124. var model = this.GetUserCookies();
  125. if (model != null)
  126. {
  127. return View(model);
  128. }
  129. return View();
  130. }
  131. /// <summary>
  132. /// 河北工大
  133. /// </summary>
  134. /// <param name="model"></param>
  135. /// <param name="returnUrl"></param>
  136. /// <returns></returns>
  137. [HttpPost]
  138. [AllowAnonymous]
  139. [ValidateAntiForgeryToken]
  140. public ActionResult HBGDLogin(LogOnModel model, string returnUrl)
  141. {
  142. if (ModelState.IsValid)
  143. {
  144. try
  145. {
  146. if (model.VerifyCode != Session["code"] as string)
  147. {
  148. ModelState.AddModelError("", "请输入正确的验证码!");
  149. return View(model);
  150. }
  151. bool isLogin;
  152. try
  153. {
  154. isLogin = UserServices.Login(model.UserName, model.Password);
  155. }
  156. catch (Exception ex)
  157. {
  158. ModelState.AddModelError("", ex.Message);
  159. return View(model);
  160. }
  161. if (isLogin)
  162. {
  163. try
  164. {
  165. this.LoginSureccessful(model, model.UserName);
  166. var referrer = HttpContext.Request.UrlReferrer;
  167. if (referrer != null)
  168. {
  169. var referrerUrl = HttpContext.Request.UrlReferrer.GetQueryStringByPath("targetUrl");
  170. if (!string.IsNullOrEmpty(referrerUrl))
  171. {
  172. return Redirect(HttpContext.Server.UrlDecode(referrerUrl));
  173. }
  174. }
  175. return RedirectToAction("Index", "Home");
  176. }
  177. catch (Exception ex)
  178. {
  179. ModelState.AddModelError("", ex.Message);
  180. return View(model);
  181. }
  182. }
  183. else
  184. {
  185. ModelState.AddModelError("", "用户名或密码不正确请检查后重新输入!");
  186. this.RemoveUserCookies(model);
  187. return View(model);
  188. }
  189. }
  190. catch (Exception ex)
  191. {
  192. //this.RemoveUserCookies(model);
  193. //ModelState.AddModelError("", "提供的用户名或密码不正确。");
  194. throw ex;
  195. }
  196. }
  197. return View(model);
  198. }
  199. /// <summary>
  200. /// 新增的登录页面
  201. /// </summary>
  202. /// <param name="returnUrl"></param>
  203. /// <returns></returns>
  204. [AllowAnonymous]
  205. public ActionResult NewLogin(string returnUrl)
  206. {
  207. ViewBag.ReturnUrl = returnUrl;
  208. var model = this.GetUserCookies();
  209. Session["FailCount"] = 0;
  210. if (model != null)
  211. {
  212. return View(model);
  213. }
  214. return View();
  215. }
  216. /// <summary>
  217. /// 新增的登录页面
  218. /// </summary>
  219. /// <param name="returnUrl"></param>
  220. /// <returns></returns>
  221. [HttpPost]
  222. [AllowAnonymous]
  223. [ValidateAntiForgeryToken]
  224. public ActionResult NewLogin(LogOnModel model, string returnUrl)
  225. {
  226. if (ModelState.IsValid)
  227. {
  228. try
  229. {
  230. int failCount = (int)Session["FailCount"];
  231. if (model.VerifyCode != "NoCode")
  232. {
  233. if (model.VerifyCode != Session["code"] as string)
  234. {
  235. failCount++;
  236. Session["FailCount"] = failCount;
  237. model.Session = Session;
  238. ModelState.AddModelError("", "请输入正确的验证码!");
  239. return View(model);
  240. }
  241. }
  242. bool isLogin;
  243. try
  244. {
  245. isLogin = UserServices.Login(model.UserName, model.Password);
  246. }
  247. catch (Exception ex)
  248. {
  249. ModelState.AddModelError("", ex.Message);
  250. return View(model);
  251. }
  252. if (isLogin)
  253. {
  254. try
  255. {
  256. this.LoginSureccessful(model, model.UserName);
  257. var referrer = HttpContext.Request.UrlReferrer;
  258. if (referrer != null)
  259. {
  260. var referrerUrl = HttpContext.Request.UrlReferrer.GetQueryStringByPath("targetUrl");
  261. if (!string.IsNullOrEmpty(referrerUrl))
  262. {
  263. return Redirect(HttpContext.Server.UrlDecode(referrerUrl));
  264. }
  265. }
  266. return RedirectToAction("Index", "Home");
  267. }
  268. catch (Exception ex)
  269. {
  270. ModelState.AddModelError("", ex.Message);
  271. return View(model);
  272. }
  273. }
  274. else
  275. {
  276. failCount++;
  277. //ViewBag.FailCount = failCount;
  278. Session["FailCount"] = failCount;
  279. model.Session = Session;
  280. //model.FailCount = failCount;
  281. ModelState.AddModelError("", "用户名或密码不正确请检查后重新输入!");
  282. //this.RemoveUserCookies(model);
  283. return View(model);
  284. }
  285. }
  286. catch (Exception ex)
  287. {
  288. //this.RemoveUserCookies(model);
  289. //ModelState.AddModelError("", "提供的用户名或密码不正确。");
  290. throw ex;
  291. }
  292. }
  293. return View(model);
  294. }
  295. /// <summary>
  296. ///
  297. /// </summary>
  298. /// <param name="returnUrl"></param>
  299. /// <returns></returns>
  300. [AllowAnonymous]
  301. public ActionResult GZZYLogin(string returnUrl)
  302. {
  303. ViewBag.ReturnUrl = returnUrl;
  304. var model = this.GetUserCookies();
  305. if (model != null)
  306. {
  307. return View(model);
  308. }
  309. return View();
  310. }
  311. /// <summary>
  312. ///
  313. /// </summary>
  314. /// <param name="model"></param>
  315. /// <param name="returnUrl"></param>
  316. /// <returns></returns>
  317. [HttpPost]
  318. [AllowAnonymous]
  319. [ValidateAntiForgeryToken]
  320. public ActionResult GZZYLogin(LogOnModel model, string returnUrl)
  321. {
  322. if (ModelState.IsValid)
  323. {
  324. try
  325. {
  326. if (model.VerifyCode != Session["code"] as string)
  327. {
  328. ModelState.AddModelError("", "请输入正确的验证码!");
  329. return View(model);
  330. }
  331. bool isLogin;
  332. try
  333. {
  334. isLogin = UserServices.Login(model.UserName, model.Password);
  335. }
  336. catch (Exception ex)
  337. {
  338. ModelState.AddModelError("", ex.Message);
  339. return View(model);
  340. }
  341. if (isLogin)
  342. {
  343. try
  344. {
  345. this.LoginSureccessful(model, model.UserName);
  346. var referrer = HttpContext.Request.UrlReferrer;
  347. if (referrer != null)
  348. {
  349. var referrerUrl = HttpContext.Request.UrlReferrer.GetQueryStringByPath("targetUrl");
  350. if (!string.IsNullOrEmpty(referrerUrl))
  351. {
  352. return Redirect(HttpContext.Server.UrlDecode(referrerUrl));
  353. }
  354. }
  355. return RedirectToAction("Index", "Home");
  356. }
  357. catch (Exception ex)
  358. {
  359. ModelState.AddModelError("", ex.Message);
  360. return View(model);
  361. }
  362. }
  363. else
  364. {
  365. ModelState.AddModelError("", "用户名或密码不正确请检查后重新输入!");
  366. this.RemoveUserCookies(model);
  367. return View(model);
  368. }
  369. }
  370. catch (Exception ex)
  371. {
  372. //this.RemoveUserCookies(model);
  373. //ModelState.AddModelError("", "提供的用户名或密码不正确。");
  374. throw ex;
  375. }
  376. }
  377. return View(model);
  378. }
  379. /// <summary>
  380. ///
  381. /// </summary>
  382. /// <returns></returns>
  383. [AllowAnonymous]
  384. public ActionResult SSOLogin()
  385. {
  386. try
  387. {
  388. var loginID = UserServices.SSOLogin();
  389. this.LoginSureccessful(new LogOnModel
  390. {
  391. UserName = loginID
  392. }, loginID);
  393. return RedirectToAction("SSOLoginJump", "Account", new { Remote_Login_User = loginID });
  394. //return RedirectToAction("SSOLoginJump", "Account", new { Remote_Login_User = loginID });
  395. }
  396. catch (Exception ex)
  397. {
  398. ViewBag.ErrorMessage = ex.Message;
  399. //filterContext.HttpContext.Response.Redirect(FormsAuthentication.LoginUrl);
  400. }
  401. return View();
  402. }
  403. /// <summary>
  404. ///
  405. /// </summary>
  406. /// <returns></returns>
  407. [AllowAnonymous]
  408. public ActionResult SSOLoginJump()
  409. {
  410. var loginID = Request["Remote_Login_User"];
  411. Session.Add("Remote_Login_User", loginID);
  412. return RedirectToAction("Index", "Home");
  413. }
  414. /// <summary>
  415. ///
  416. /// </summary>
  417. /// <returns></returns>
  418. [NoSSO]
  419. public ActionResult LogOff()
  420. {
  421. var cookieName = EMIS.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME;
  422. var logoffUrl = Url.Content(EMIS.Utility.Const.LOCAL_SETTING_LOGOFF_PAGE);
  423. HttpCookie cookie = new HttpCookie(cookieName);
  424. cookie.Value = "";
  425. cookie.Expires = DateTime.Now.AddDays(-1);
  426. Response.Clear();
  427. Response.AppendCookie(cookie);
  428. //if (Const.SSO_IS_SSO_LOGIN)
  429. //{
  430. // SSO.SSOLogout();
  431. //}
  432. var url = Request.QueryString["url"];
  433. if (!string.IsNullOrEmpty(url))
  434. {
  435. logoffUrl = logoffUrl += "?url=" + url;
  436. }
  437. return Redirect(logoffUrl);
  438. //return Redirect("http://portal.gdsspt.cn/c/portal/logout?service=http://localhost:2583/Account/Login");
  439. }
  440. /// <summary>
  441. ///
  442. /// </summary>
  443. /// <param name="loginID"></param>
  444. /// <returns></returns>
  445. [AllowAnonymous]
  446. public ActionResult ForgotPassword(string loginID)
  447. {
  448. return View();
  449. }
  450. /// <summary>
  451. ///
  452. /// </summary>
  453. /// <returns></returns>
  454. [AllowAnonymous]
  455. [HttpPost]
  456. [ValidateAntiForgeryToken]
  457. public ActionResult ForgotPassword()
  458. {
  459. var result = new ReturnMessage { IsSuccess = true, Message = "邮件发送成功,请查收。" };
  460. string loginID = Request.Form["LoginID"];
  461. string failUrl = Url.Content("~/Account/ForgotPassword?LoginID=" + loginID);
  462. string successUrl = Url.Content("~/Account/Login");
  463. var user = UserServices.GetUserByLoginID(loginID);
  464. if (user == null)
  465. {
  466. result.IsSuccess = false;
  467. result.Message = "用户名输入错误。";
  468. return Json(result);
  469. }
  470. var url = HttpContext.Request.Url.Scheme + "://" + HttpContext.Request.Url.Host + ":" + HttpContext.Request.Url.Port + Url.Content("~/").TrimEnd('/');
  471. try
  472. {
  473. var email = UserServices.GetUserEmail(user.UserID);
  474. if (email == null)
  475. {
  476. result.IsSuccess = false;
  477. result.Message = "用户并没有注册电子邮箱信息。";
  478. return Json(result);
  479. }
  480. MailVerifyServices.SendVerifyMail(url, user.UserID, email);
  481. return Json(result);
  482. }
  483. catch (Exception ex)
  484. {
  485. result.IsSuccess = false;
  486. result.Message = ex.Message;
  487. return Json(result);
  488. }
  489. }
  490. /// <summary>
  491. /// 新增忘记密码页面
  492. /// </summary>
  493. /// <param name="loginID"></param>
  494. /// <returns></returns>
  495. [AllowAnonymous]
  496. public ActionResult NewForgotPassword(string loginID)
  497. {
  498. return View();
  499. }
  500. /// <summary>
  501. /// 新增忘记密码页面
  502. /// </summary>
  503. /// <returns></returns>
  504. [AllowAnonymous]
  505. [HttpPost]
  506. [ValidateAntiForgeryToken]
  507. public ActionResult NewForgotPassword()
  508. {
  509. var result = new ReturnMessage { IsSuccess = true, Message = "邮件发送成功,请查收。" };
  510. string loginID = Request.Form["LoginID"];
  511. string failUrl = Url.Content("~/Account/NewForgotPassword?LoginID=" + loginID);
  512. string successUrl = Url.Content("~/Account/NewLogin");
  513. var user = UserServices.GetUserByLoginID(loginID);
  514. if (user == null)
  515. {
  516. result.IsSuccess = false;
  517. result.Message = "用户名输入错误。";
  518. return Json(result);
  519. }
  520. var url = HttpContext.Request.Url.Scheme + "://" + HttpContext.Request.Url.Host + ":" + HttpContext.Request.Url.Port + Url.Content("~/").TrimEnd('/');
  521. try
  522. {
  523. var email = UserServices.GetUserEmail(user.UserID);
  524. if (email == null)
  525. {
  526. result.IsSuccess = false;
  527. result.Message = "用户并没有注册电子邮箱信息。";
  528. return Json(result);
  529. }
  530. MailVerifyServices.SendVerifyMail(url, user.UserID, email);
  531. return Json(result);
  532. }
  533. catch (Exception ex)
  534. {
  535. result.IsSuccess = false;
  536. result.Message = ex.Message;
  537. return Json(result);
  538. }
  539. }
  540. /// <summary>
  541. ///
  542. /// </summary>
  543. /// <returns></returns>
  544. public ActionResult ChangePassword()
  545. {
  546. return View();
  547. }
  548. /// <summary>
  549. ///
  550. /// </summary>
  551. /// <param name="changePasswordView"></param>
  552. /// <returns></returns>
  553. [HttpPost]
  554. public ActionResult ChangePassword(ChangePasswordView changePasswordView)
  555. {
  556. try
  557. {
  558. var principal = ((EMIS.Utility.FormValidate.CustomPrincipal)HttpContext.User);
  559. var loginID = principal.LoginID;
  560. var user = UserServices.GetUserByLoginID(loginID);
  561. if (user.Password.ToLower() != changePasswordView.OldPassword.MD5().ToLower())
  562. {
  563. return Json(new ReturnMessage()
  564. {
  565. IsSuccess = false,
  566. Message = "保存失败:原密码验证失败"
  567. });
  568. }
  569. UserServices.ChangePassword(changePasswordView, user.UserID);
  570. return Json(new ReturnMessage()
  571. {
  572. IsSuccess = true,
  573. Message = "密码已经成功修改!"
  574. });
  575. }
  576. catch (Exception ex)
  577. {
  578. return Json(new ReturnMessage()
  579. {
  580. IsSuccess = false,
  581. Message = "保存失败:" + ex.Message
  582. });
  583. }
  584. }
  585. /// <summary>
  586. ///
  587. /// </summary>
  588. /// <param name="url"></param>
  589. /// <returns></returns>
  590. public ActionResult GetTicket(string url)
  591. {
  592. FormsAuthenticationHelper fa = new FormsAuthenticationHelper();
  593. fa.GetTicket(System.Web.HttpContext.Current, url);
  594. return Json(true);
  595. }
  596. /// <summary>
  597. ///
  598. /// </summary>
  599. /// <param name="ticket"></param>
  600. /// <returns></returns>
  601. [HttpPost]
  602. [AllowAnonymous]
  603. public ActionResult AuthTicket(string ticket)
  604. {
  605. FormsAuthenticationHelper fa = new FormsAuthenticationHelper();
  606. EMIS.Utility.FormValidate.CustomPrincipal user = (EMIS.Utility.FormValidate.CustomPrincipal)fa.ValidateTitcket(System.Web.HttpContext.Current, ticket);
  607. return Json(user.LoginID);
  608. }
  609. /// <summary>
  610. /// 录取名单页面
  611. /// </summary>
  612. /// <returns></returns>
  613. [HttpGet]
  614. [AllowAnonymous]
  615. public ActionResult NewStudentEnter()
  616. {
  617. //ViewBag.ReturnUrl = returnUrl;
  618. var model = this.GetUserCookies();
  619. if (model != null)
  620. {
  621. return View(model);
  622. }
  623. return View();
  624. }
  625. /// <summary>
  626. /// 录取名单页面列表查询
  627. /// </summary>
  628. /// <param name="pararms"></param>
  629. /// <returns></returns>
  630. [HttpPost]
  631. [AllowAnonymous]
  632. //[ValidateAntiForgeryToken]
  633. public ActionResult NewStudentEnter(string examineeNum, string verifyCode, string userName)
  634. {
  635. if (verifyCode != Session["code"] as string)
  636. {
  637. //ModelState.AddModelError("", "请输入正确的验证码!");
  638. return base.Json("错误");
  639. }
  640. var newStudentEnterView = newStudentEnterServices.GetNewStudentEnterView(examineeNum, userName);
  641. return base.Json(newStudentEnterView);
  642. }
  643. /// <summary>
  644. /// 录取通知书打印
  645. /// </summary>
  646. /// <returns></returns>
  647. [HttpGet]
  648. [AllowAnonymous]
  649. public ActionResult Report()
  650. {
  651. return View();
  652. }
  653. }
  654. }