AccountController.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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 EMIS.Web.Filters;
  12. using EMIS.Web.Models;
  13. using Bowin.Common;
  14. using Bowin.Common.Utility;
  15. using EMIS.ViewModel.Account;
  16. using EMIS.CommonLogic.SystemServices;
  17. using EMIS.ViewModel.SystemView;
  18. using EMIS.ViewModel;
  19. using EMIS.Web.Controls;
  20. using EMIS.Utility.SSO;
  21. using EMIS.Utility;
  22. using EMIS.ViewModel.Students;
  23. using EMIS.CommonLogic.Students;
  24. using Bowin.Web.Controls.Mvc;
  25. using System.IO;
  26. namespace EMIS.Web.Controllers
  27. {
  28. //[Authorize]
  29. [Authorization]
  30. public class AccountController : Controller
  31. {
  32. public IUserServices UserServices { get; set; }
  33. public IStudentsServices StudentsServices { get; set; }
  34. public IMailVerifyServices MailVerifyServices { get; set; }
  35. public IParameterServices ParameterServices { get; set; }
  36. //
  37. // GET: /Account/Login
  38. [AllowAnonymous]
  39. public ActionResult SSOLogin()
  40. {
  41. try
  42. {
  43. var loginID = UserServices.SSOLogin();
  44. this.LoginSureccessful(new LogOnModel
  45. {
  46. UserName = loginID
  47. }, loginID);
  48. return RedirectToAction("SSOLoginJump", "Account", new { Remote_Login_User = loginID });
  49. //return RedirectToAction("SSOLoginJump", "Account", new { Remote_Login_User = loginID });
  50. }
  51. catch (Exception ex)
  52. {
  53. ViewBag.ErrorMessage = ex.Message;
  54. //filterContext.HttpContext.Response.Redirect(FormsAuthentication.LoginUrl);
  55. }
  56. return View();
  57. }
  58. [AllowAnonymous]
  59. public ActionResult SSOLoginJump()
  60. {
  61. var loginID = Request["Remote_Login_User"];
  62. Session.Add("Remote_Login_User", loginID);
  63. return RedirectToAction("Index", "Home");
  64. }
  65. [AllowAnonymous]
  66. public ActionResult Login(string returnUrl)
  67. {
  68. ViewBag.ReturnUrl = returnUrl;
  69. var model = this.GetUserCookies();
  70. if (model != null)
  71. {
  72. return View(model);
  73. }
  74. return View();
  75. }
  76. [HttpPost]
  77. [AllowAnonymous]
  78. public ActionResult GetLoginTip()
  79. {
  80. var loginTip = ParameterServices.GetParameterValue(CF_ParameterType.LoginTip);
  81. loginTip = Server.HtmlDecode(loginTip);
  82. return Json(loginTip);
  83. }
  84. //
  85. // POST: /Account/Login
  86. [HttpPost]
  87. [AllowAnonymous]
  88. public ActionResult Login(LogOnModel model, string returnUrl)
  89. {
  90. if (ModelState.IsValid)
  91. {
  92. try
  93. {
  94. if (model.VerifyCode != Session["code"] as string)
  95. {
  96. ModelState.AddModelError("", "请输入正确的验证码!");
  97. return View(model);
  98. }
  99. bool isLogin;
  100. try
  101. {
  102. isLogin = UserServices.Login(model.UserName, model.Password);
  103. }
  104. catch (Exception ex)
  105. {
  106. ModelState.AddModelError("", ex.Message);
  107. return View(model);
  108. }
  109. bool isCanLogin = UserServices.LoginHistory(model.UserName, isLogin);
  110. if (!isCanLogin)
  111. {
  112. throw new Exception("当天登陆失败次数超过5次,请明天再试或联系管理员。");
  113. }
  114. if (isLogin)
  115. {
  116. try
  117. {
  118. this.LoginSureccessful(model, model.UserName);
  119. var referrer = HttpContext.Request.UrlReferrer;
  120. if (referrer != null)
  121. {
  122. var referrerUrl = HttpContext.Request.UrlReferrer.GetQueryStringByPath("targetUrl");
  123. if (!string.IsNullOrEmpty(referrerUrl))
  124. {
  125. return Redirect(HttpContext.Server.UrlDecode(referrerUrl));
  126. }
  127. }
  128. return RedirectToAction("Index", "Home");
  129. }
  130. catch (Exception ex)
  131. {
  132. ModelState.AddModelError("", ex.Message);
  133. return View(model);
  134. }
  135. }
  136. else
  137. {
  138. ModelState.AddModelError("", "密码错误或不存在该账户!(注意:如密码连续输入错误5次以上账号将被锁定)");
  139. this.RemoveUserCookies(model);
  140. return View(model);
  141. }
  142. }
  143. catch (Exception ex)
  144. {
  145. return RedirectToAction("MsgShow", "Common", new
  146. {
  147. WindowID = Request["WindowID"],
  148. msg = ex.Message,
  149. url = Url.Action("Login")
  150. });
  151. //this.RemoveUserCookies(model);
  152. //ModelState.AddModelError("", "提供的用户名或密码不正确。");
  153. //throw ex;
  154. }
  155. }
  156. return View(model);
  157. }
  158. /// <summary>
  159. /// 新增的登录页面
  160. /// </summary>
  161. /// <param name="returnUrl"></param>
  162. /// <returns></returns>
  163. [AllowAnonymous]
  164. public ActionResult NewLogin(string returnUrl)
  165. {
  166. ViewBag.ReturnUrl = returnUrl;
  167. var model = this.GetUserCookies();
  168. Session["FailCount"] = 0;
  169. if (model != null)
  170. {
  171. return View(model);
  172. }
  173. return View();
  174. }
  175. /// <summary>
  176. /// 新增的登录页面
  177. /// </summary>
  178. /// <param name="returnUrl"></param>
  179. /// <returns></returns>
  180. [HttpPost]
  181. [AllowAnonymous]
  182. public ActionResult NewLogin(LogOnModel model, string returnUrl)
  183. {
  184. if (ModelState.IsValid)
  185. {
  186. try
  187. {
  188. int failCount = (int)Session["FailCount"];
  189. if (model.VerifyCode != "NoCode")
  190. {
  191. if (model.VerifyCode != Session["code"] as string)
  192. {
  193. failCount++;
  194. Session["FailCount"] = failCount;
  195. model.Session = Session;
  196. ModelState.AddModelError("", "请输入正确的验证码!");
  197. return View(model);
  198. }
  199. }
  200. bool isLogin;
  201. try
  202. {
  203. isLogin = UserServices.Login(model.UserName, model.Password);
  204. }
  205. catch (Exception ex)
  206. {
  207. ModelState.AddModelError("", ex.Message);
  208. return View(model);
  209. }
  210. if (isLogin)
  211. {
  212. try
  213. {
  214. this.LoginSureccessful(model, model.UserName);
  215. var referrer = HttpContext.Request.UrlReferrer;
  216. if (referrer != null)
  217. {
  218. var referrerUrl = HttpContext.Request.UrlReferrer.GetQueryStringByPath("targetUrl");
  219. if (!string.IsNullOrEmpty(referrerUrl))
  220. {
  221. return Redirect(HttpContext.Server.UrlDecode(referrerUrl));
  222. }
  223. }
  224. return RedirectToAction("Index", "Home");
  225. }
  226. catch (Exception ex)
  227. {
  228. ModelState.AddModelError("", ex.Message);
  229. return View(model);
  230. }
  231. }
  232. else
  233. {
  234. failCount++;
  235. //ViewBag.FailCount = failCount;
  236. Session["FailCount"] = failCount;
  237. model.Session = Session;
  238. //model.FailCount = failCount;
  239. ModelState.AddModelError("", "用户名或密码不正确请检查后重新输入!");
  240. //this.RemoveUserCookies(model);
  241. return View(model);
  242. }
  243. }
  244. catch (Exception ex)
  245. {
  246. //this.RemoveUserCookies(model);
  247. //ModelState.AddModelError("", "提供的用户名或密码不正确。");
  248. throw ex;
  249. }
  250. }
  251. return View(model);
  252. }
  253. [AllowAnonymous]
  254. public ActionResult GZZYLogin(string returnUrl)
  255. {
  256. ViewBag.ReturnUrl = returnUrl;
  257. var model = this.GetUserCookies();
  258. if (model != null)
  259. {
  260. return View(model);
  261. }
  262. return View();
  263. }
  264. //
  265. // POST: /Account/Login
  266. [HttpPost]
  267. [AllowAnonymous]
  268. public ActionResult GZZYLogin(LogOnModel model, string returnUrl)
  269. {
  270. if (ModelState.IsValid)
  271. {
  272. try
  273. {
  274. if (model.VerifyCode != Session["code"] as string)
  275. {
  276. ModelState.AddModelError("", "请输入正确的验证码!");
  277. return View(model);
  278. }
  279. bool isLogin;
  280. try
  281. {
  282. isLogin = UserServices.Login(model.UserName, model.Password);
  283. }
  284. catch (Exception ex)
  285. {
  286. ModelState.AddModelError("", ex.Message);
  287. return View(model);
  288. }
  289. if (isLogin)
  290. {
  291. try
  292. {
  293. this.LoginSureccessful(model, model.UserName);
  294. var referrer = HttpContext.Request.UrlReferrer;
  295. if (referrer != null)
  296. {
  297. var referrerUrl = HttpContext.Request.UrlReferrer.GetQueryStringByPath("targetUrl");
  298. if (!string.IsNullOrEmpty(referrerUrl))
  299. {
  300. return Redirect(HttpContext.Server.UrlDecode(referrerUrl));
  301. }
  302. }
  303. return RedirectToAction("Index", "Home");
  304. }
  305. catch (Exception ex)
  306. {
  307. ModelState.AddModelError("", ex.Message);
  308. return View(model);
  309. }
  310. }
  311. else
  312. {
  313. ModelState.AddModelError("", "用户名或密码不正确请检查后重新输入!");
  314. this.RemoveUserCookies(model);
  315. return View(model);
  316. }
  317. }
  318. catch (Exception ex)
  319. {
  320. //this.RemoveUserCookies(model);
  321. //ModelState.AddModelError("", "提供的用户名或密码不正确。");
  322. throw ex;
  323. }
  324. }
  325. return View(model);
  326. }
  327. //
  328. // POST: /Account/LogOff
  329. [NoSSO]
  330. public ActionResult LogOff()
  331. {
  332. var cookieName = EMIS.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME;
  333. var logoffUrl = Url.Content(EMIS.Utility.Const.LOCAL_SETTING_LOGOFF_PAGE);
  334. HttpCookie cookie = new HttpCookie(cookieName);
  335. cookie.Value = "";
  336. cookie.Expires = DateTime.Now.AddDays(-1);
  337. Response.Clear();
  338. Response.AppendCookie(cookie);
  339. //if (Const.SSO_IS_SSO_LOGIN)
  340. //{
  341. // SSO.SSOLogout();
  342. //}
  343. var url = Request.QueryString["url"];
  344. if (!string.IsNullOrEmpty(url))
  345. {
  346. logoffUrl = logoffUrl += "?url=" + url;
  347. }
  348. return Redirect(logoffUrl);
  349. //return Redirect("http://portal.gdsspt.cn/c/portal/logout?service=http://localhost:2583/Account/Login");
  350. }
  351. [AllowAnonymous]
  352. public ActionResult ForgotPassword(string loginID)
  353. {
  354. Random r = new Random(DateTime.Now.Second);
  355. ViewBag.VKey = r.Next().ToString().MD5();
  356. Session["__VKey"] = ViewBag.VKey;
  357. return View();
  358. }
  359. [AllowAnonymous]
  360. [HttpPost]
  361. [ValidateAntiForgeryToken]
  362. public ActionResult ForgotPassword()
  363. {
  364. if (Request.Form["VKey"].ToString() != Session["__VKey"].ToString())
  365. {
  366. throw new Exception("注意,您正从不安全的网站访问当前页面。");
  367. }
  368. if (Request.UrlReferrer.Host != Request.Url.Host)
  369. {
  370. throw new Exception("注意,您正从不安全的网站访问当前页面。");
  371. }
  372. var result = new ReturnMessage { IsSuccess = true, Message = "邮件发送成功,请查收。" };
  373. string loginID = Request.Form["LoginID"];
  374. string failUrl = Url.Content("~/Account/ForgotPassword?LoginID=" + loginID);
  375. string successUrl = Url.Content("~/Account/Login");
  376. var user = UserServices.GetUserByLoginID(loginID);
  377. if (user == null)
  378. {
  379. result.IsSuccess = false;
  380. result.Message = "用户名输入错误。";
  381. return Json(result);
  382. }
  383. var url = HttpContext.Request.Url.Scheme + "://" + HttpContext.Request.Url.Host + ":" + HttpContext.Request.Url.Port + Url.Content("~/").TrimEnd('/');
  384. try
  385. {
  386. var email = UserServices.GetUserEmail(user.UserID);
  387. if (email == null)
  388. {
  389. result.IsSuccess = false;
  390. result.Message = "用户并没有注册电子邮箱信息。";
  391. return Json(result);
  392. }
  393. MailVerifyServices.SendVerifyMail(url, user.UserID, email);
  394. return Json(result);
  395. }
  396. catch (Exception ex)
  397. {
  398. result.IsSuccess = false;
  399. result.Message = ex.Message;
  400. return Json(result);
  401. }
  402. }
  403. /// <summary>
  404. /// 新增忘记密码页面
  405. /// </summary>
  406. /// <param name="loginID"></param>
  407. /// <returns></returns>
  408. [AllowAnonymous]
  409. public ActionResult NewForgotPassword(string loginID)
  410. {
  411. return View();
  412. }
  413. /// <summary>
  414. /// 新增忘记密码页面
  415. /// </summary>
  416. /// <returns></returns>
  417. [AllowAnonymous]
  418. [HttpPost]
  419. public ActionResult NewForgotPassword()
  420. {
  421. var result = new ReturnMessage { IsSuccess = true, Message = "邮件发送成功,请查收。" };
  422. string loginID = Request.Form["LoginID"];
  423. string failUrl = Url.Content("~/Account/NewForgotPassword?LoginID=" + loginID);
  424. string successUrl = Url.Content("~/Account/NewLogin");
  425. var user = UserServices.GetUserByLoginID(loginID);
  426. if (user == null)
  427. {
  428. result.IsSuccess = false;
  429. result.Message = "用户名输入错误。";
  430. return Json(result);
  431. }
  432. var url = HttpContext.Request.Url.Scheme + "://" + HttpContext.Request.Url.Host + ":" + HttpContext.Request.Url.Port + Url.Content("~/").TrimEnd('/');
  433. try
  434. {
  435. var email = UserServices.GetUserEmail(user.UserID);
  436. if (email == null)
  437. {
  438. result.IsSuccess = false;
  439. result.Message = "用户并没有注册电子邮箱信息。";
  440. return Json(result);
  441. }
  442. MailVerifyServices.SendVerifyMail(url, user.UserID, email);
  443. return Json(result);
  444. }
  445. catch (Exception ex)
  446. {
  447. result.IsSuccess = false;
  448. result.Message = ex.Message;
  449. return Json(result);
  450. }
  451. }
  452. public ActionResult ChangePassword()
  453. {
  454. return View();
  455. }
  456. [HttpPost]
  457. public ActionResult ChangePassword(ChangePasswordView changePasswordView)
  458. {
  459. try
  460. {
  461. var principal = ((EMIS.Utility.FormValidate.CustomPrincipal)HttpContext.User);
  462. var loginID = principal.LoginID;
  463. var user = UserServices.GetUserByLoginID(loginID);
  464. if (user.Password.ToLower() != changePasswordView.OldPassword.MD5().ToLower())
  465. {
  466. return Json(new ReturnMessage()
  467. {
  468. IsSuccess = false,
  469. Message = "保存失败:原密码验证失败"
  470. });
  471. }
  472. UserServices.ChangePassword(changePasswordView, user.UserID);
  473. return Json(new ReturnMessage()
  474. {
  475. IsSuccess = true,
  476. Message = "密码已经成功修改!"
  477. });
  478. }
  479. catch (Exception ex)
  480. {
  481. return Json(new ReturnMessage()
  482. {
  483. IsSuccess = false,
  484. Message = "保存失败:" + ex.Message
  485. });
  486. }
  487. }
  488. public ActionResult GetTicket(string url)
  489. {
  490. FormsAuthenticationHelper fa = new FormsAuthenticationHelper();
  491. fa.GetTicket(System.Web.HttpContext.Current, url);
  492. return Json(true);
  493. }
  494. [HttpPost]
  495. [AllowAnonymous]
  496. public ActionResult AuthTicket(string ticket)
  497. {
  498. FormsAuthenticationHelper fa = new FormsAuthenticationHelper();
  499. EMIS.Utility.FormValidate.CustomPrincipal user = (EMIS.Utility.FormValidate.CustomPrincipal)fa.ValidateTitcket(System.Web.HttpContext.Current, ticket);
  500. return Json(user.LoginID);
  501. }
  502. [HttpGet]
  503. [AllowAnonymous]
  504. public ActionResult Regist()
  505. {
  506. Random r = new Random(DateTime.Now.Second);
  507. ViewBag.VKey = r.Next().ToString().MD5();
  508. Session["__VKey"] = ViewBag.VKey;
  509. return View(new RegistView());
  510. }
  511. [HttpPost]
  512. [AllowAnonymous]
  513. public ActionResult GetSchoolAreaWithoutSocial(DropdownListBindType? bindType)
  514. {
  515. List<DropdownListItem> list = DictionaryHelper.GetDictionaryValue(DictionaryItem.CF_SchoolArea)
  516. .Where(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE && x.Value < 3)
  517. .Select(x => new DropdownListItem { Text = x.Name, Value = x.Value.ToString() }).ToList();
  518. DropdownListBindType dbt = bindType == null ? DropdownListBindType.SelectAll : bindType.Value;
  519. DropdownList.FormatDropdownItemList(dbt, list);
  520. return base.Json(list);
  521. }
  522. [HttpPost]
  523. [AllowAnonymous]
  524. [ValidateAntiForgeryToken]
  525. public ActionResult Regist(RegistView registView)
  526. {
  527. if (Request.Form["VKey"].ToString() != Session["__VKey"].ToString())
  528. {
  529. throw new Exception("注意,您正从不安全的网站访问当前页面。");
  530. }
  531. if (Request.UrlReferrer.Host != Request.Url.Host)
  532. {
  533. throw new Exception("注意,您正从不安全的网站访问当前页面。");
  534. }
  535. try
  536. {
  537. var accepts = new List<string> { ".jpg" };
  538. var postedFile = Request.Files["PhotoUrl"];
  539. if (postedFile != null && !string.IsNullOrEmpty(postedFile.FileName) && !accepts.Contains(Path.GetExtension(postedFile.FileName).ToLower()))
  540. {
  541. throw new Exception("只允许上传.jpg格式的文件。");
  542. }
  543. if (postedFile != null && !string.IsNullOrEmpty(postedFile.FileName) && (postedFile.ContentLength > (200 * 1024) || postedFile.ContentLength < (50 * 1024)))
  544. {
  545. throw new Exception("只允许上传50-200k大小的照片。");
  546. }
  547. string photoUrl = FileUploadHelper.UploadFile(postedFile);
  548. if (photoUrl != null)
  549. {
  550. registView.PhotoUrl = photoUrl;
  551. }
  552. var userID = UserServices.StudentRegist(registView);
  553. this.LoginSureccessful(new LogOnModel { UserName = registView.UserName, Password = registView.PCode, RememberMe = false }, registView.UserName, userID);
  554. string scripts = "<script>alert('注册成功。'); window.top.location.href = '../Home/Index';</script>";
  555. return Content(scripts);
  556. }
  557. catch (Exception ex)
  558. {
  559. return RedirectToAction("MsgShow", "Common", new
  560. {
  561. WindowID = Request["WindowID"],
  562. msg = "注册失败:" + ex.Message
  563. });
  564. }
  565. }
  566. [HttpGet]
  567. public ActionResult FinishInfomation()
  568. {
  569. var user = (EMIS.Utility.FormValidate.CustomPrincipal)HttpContext.User;
  570. var registView = StudentsServices.GetRegistViewByUserID(user.UserID);
  571. return View(registView);
  572. }
  573. [HttpPost]
  574. public ActionResult FinishInfomation(RegistView registView)
  575. {
  576. try
  577. {
  578. var accepts = new List<string> { ".jpg" };
  579. var postedFile = Request.Files["PhotoUrl"];
  580. if (postedFile != null && !string.IsNullOrEmpty(postedFile.FileName) && !accepts.Contains(Path.GetExtension(postedFile.FileName).ToLower()))
  581. {
  582. throw new Exception("只允许上传.jpg格式的文件。");
  583. }
  584. if (postedFile != null && !string.IsNullOrEmpty(postedFile.FileName) && (postedFile.ContentLength > (200 * 1024) || postedFile.ContentLength < (50 * 1024)))
  585. {
  586. throw new Exception("只允许上传50-200k大小的照片。");
  587. }
  588. string photoUrl = FileUploadHelper.UploadFile(postedFile);
  589. if (photoUrl != null)
  590. {
  591. registView.PhotoUrl = photoUrl;
  592. }
  593. UserServices.StudentRegist(registView);
  594. string scripts = "<script>alert('保存成功。'); window.top.location.href = '../Home/Index';</script>";
  595. return Content(scripts);
  596. }
  597. catch (Exception ex)
  598. {
  599. return RedirectToAction("MsgShow", "Common", new
  600. {
  601. msg = "保存失败:" + ex.Message,
  602. url = Url.Action("FinishInfomation")
  603. });
  604. }
  605. }
  606. }
  607. }