BaseExtensions.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using Autofac;
  6. using Bowin.Common;
  7. using EMISOnline.Utility;
  8. using EMISOnline.CommonLogic.SystemServices;
  9. using EMISOnline.ViewModel.Account;
  10. using System.Web.Mvc;
  11. using EMISOnline.ViewModel.SystemView;
  12. namespace EMISOnline.Web.Controls
  13. {
  14. public static class BaseExtensions
  15. {
  16. public static void GetContextUser()
  17. {
  18. FormsAuthenticationHelper fahelper = new FormsAuthenticationHelper();
  19. var cookieName = EMISOnline.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME;
  20. fahelper.AuthenticateRequest(HttpContext.Current, cookieName, Const.LOCAL_AUTH_EXCEPTURL);
  21. }
  22. public static void LoginSureccessful(this ControllerBase controller, LogOnModel model, string userName)
  23. {
  24. FormsAuthenticationHelper fahelper = new FormsAuthenticationHelper();
  25. var cookieName = EMISOnline.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME;
  26. List<string> userData = new List<string>();
  27. UserView userpageResult;
  28. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  29. {
  30. IUserServices userServices = scope.Resolve<IUserServices>();
  31. userpageResult = userServices.GetUserByLoginID(userName, true);
  32. if (userpageResult == null)
  33. {
  34. throw new Exception("获取用户失败。");
  35. }
  36. }
  37. if (model.RememberMe)
  38. {
  39. controller.RemeberUserCookies(model);
  40. }
  41. else
  42. {
  43. controller.RemoveUserCookies(model);
  44. }
  45. userData.Add(userpageResult.LoginID);
  46. userData.Add(userpageResult.UserID.ToString());
  47. userData.Add(userpageResult.Name);
  48. userData.Add(userpageResult.StaffID.HasValue ? userpageResult.StaffID.Value.ToString() : "");//教职工ID
  49. userData.Add(userpageResult.StudentID.HasValue ? userpageResult.StudentID.Value.ToString() : "");//学生ID
  50. userData.Add(userpageResult.DepartmentID.HasValue ? userpageResult.DepartmentID.Value.ToString() : "");//部门(教研室)ID
  51. userData.Add(userpageResult.DepartmentName ?? "");//部门(教研室)树结构ID
  52. userData.Add(userpageResult.HierarchyID ?? "");//部门(教研室)名称
  53. userData.Add(userpageResult.CollegeID.HasValue ? userpageResult.CollegeID.Value.ToString() : "");
  54. userData.Add(userpageResult.CampusID.HasValue ? userpageResult.CampusID.Value.ToString() : "");
  55. userData.Add(userpageResult.UniversityID.HasValue ? userpageResult.UniversityID.Value.ToString() : "");
  56. userData.Add(userpageResult.RoleID.HasValue ? userpageResult.RoleID.Value.ToString() : "");
  57. fahelper.loginFormsAuthentication(controller.ControllerContext.HttpContext, cookieName, userData);
  58. }
  59. public static void RemoveUserCookies(this ControllerBase controller, LogOnModel model)
  60. {
  61. HttpCookie cookie = new HttpCookie("username");
  62. cookie.Value = model.UserName;
  63. cookie.Expires = DateTime.Now.AddDays(-1);
  64. controller.ControllerContext.HttpContext.Response.AppendCookie(cookie);
  65. HttpCookie cookie2 = new HttpCookie("password");
  66. cookie2.Value = model.Password;
  67. cookie2.Expires = DateTime.Now.AddDays(-1);
  68. controller.ControllerContext.HttpContext.Response.AppendCookie(cookie2);
  69. HttpCookie cookie3 = new HttpCookie("rememberme");
  70. cookie3.Value = model.RememberMe.ToString();
  71. cookie3.Expires = DateTime.Now.AddDays(-1);
  72. controller.ControllerContext.HttpContext.Response.AppendCookie(cookie3);
  73. }
  74. public static void RemeberUserCookies(this ControllerBase controller, LogOnModel model)
  75. {
  76. HttpCookie cookie = new HttpCookie("username");
  77. cookie.Value = model.UserName;
  78. cookie.Expires = DateTime.Now.AddDays(1);
  79. controller.ControllerContext.HttpContext.Response.AppendCookie(cookie);
  80. HttpCookie cookie2 = new HttpCookie("password");
  81. cookie2.Value = model.Password;
  82. cookie2.Expires = DateTime.Now.AddDays(1);
  83. controller.ControllerContext.HttpContext.Response.AppendCookie(cookie2);
  84. HttpCookie cookie3 = new HttpCookie("rememberme");
  85. cookie3.Value = model.RememberMe.ToString();
  86. cookie3.Expires = DateTime.Now.AddDays(1);
  87. controller.ControllerContext.HttpContext.Response.AppendCookie(cookie3);
  88. }
  89. }
  90. }