Authorization.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System.Web.Mvc;
  2. using System.Web.Security;
  3. using Bowin.Common;
  4. using System.Collections.Generic;
  5. using System.Web;
  6. using System.Linq;
  7. using Autofac;
  8. using EMIS.Utility;
  9. using EMIS.Utility.SSO;
  10. using EMIS.ViewModel.Account;
  11. using EMIS.Web.Controls;
  12. using EMIS.CommonLogic.SystemServices;
  13. using System.Web.Routing;
  14. using EMIS.Web.Controllers;
  15. using EMIS.CommonLogic.Students;
  16. namespace System
  17. {
  18. /// <summary>
  19. /// 表示需要用户登录才可以使用的特性
  20. /// 如果不需要处理用户登录,则请指定AllowAnonymousAttribute属性
  21. /// </summary>
  22. [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
  23. public class AuthorizationAttribute : AuthorizeAttribute, IAuthorizationFilter
  24. {
  25. public void OnAuthorization(AuthorizationContext filterContext)
  26. {
  27. if (filterContext.HttpContext == null)
  28. {
  29. throw new Exception("此特性只适合于Web应用程序使用!");
  30. }
  31. else
  32. {
  33. var allowAnonymousType = filterContext.ActionDescriptor.GetCustomAttributes(typeof(AllowAnonymousAttribute), false).FirstOrDefault();
  34. if (allowAnonymousType != null)
  35. {
  36. return;
  37. }
  38. FormsAuthenticationHelper fahelper = new FormsAuthenticationHelper();
  39. //var noSSOAttribute = filterContext.ActionDescriptor.GetCustomAttributes(typeof(NoSSOAttribute), false)
  40. // .FirstOrDefault();
  41. var cookieName = EMIS.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME;
  42. var sessionUser = filterContext.HttpContext.Session["Remote_Login_User"]; //filterContext.HttpContext.Session["Remote_Login_User"];
  43. if (filterContext.HttpContext.Request.Cookies[cookieName] == null && sessionUser != null)
  44. {
  45. filterContext.Controller.LoginSureccessful(new LogOnModel { UserName = sessionUser.ToString() }, sessionUser.ToString());
  46. //filterContext.HttpContext.Session.Remove("Remote_Login_User");
  47. }
  48. fahelper.AuthenticateRequest(HttpContext.Current, cookieName, Const.LOCAL_AUTH_EXCEPTURL);
  49. var userInfo = EMIS.Utility.FormValidate.CustomPrincipal.Current;
  50. if (userInfo != null && userInfo.NeedChangePwd == true
  51. && !filterContext.HttpContext.Request.Path.Contains("/Home/MainChangePwd")
  52. && !filterContext.HttpContext.Request.Path.Contains("/Account/ChangePassword")
  53. && !filterContext.HttpContext.Request.Path.Contains("/Account/Login")
  54. && !filterContext.HttpContext.Request.Path.Contains("/Account/LogOff"))
  55. {
  56. filterContext.HttpContext.Response.RedirectPermanent("~/Home/MainChangePwd", true);
  57. return;
  58. }
  59. if (userInfo != null && userInfo.IsStudent)
  60. {
  61. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  62. {
  63. if (!filterContext.HttpContext.Request.Path.Contains("/Account/FinishInfomation")
  64. && !filterContext.HttpContext.Request.Path.Contains("/Home/MainChangePwd")
  65. && !filterContext.HttpContext.Request.Path.Contains("/Account/ChangePassword")
  66. && !filterContext.HttpContext.Request.Path.Contains("/Home/Index")
  67. && !filterContext.HttpContext.Request.Path.Contains("/Common/")
  68. && !filterContext.HttpContext.Request.Path.Contains("/GetUserRoleDropdownList")
  69. && filterContext.HttpContext.Request.Path != "/"
  70. && !filterContext.HttpContext.Request.Path.Contains("/Account/LogOff"))
  71. {
  72. IStudentsServices studentsServices = scope.Resolve<IStudentsServices>();
  73. if (!studentsServices.CheckNecessary(userInfo.UserID))
  74. {
  75. filterContext.HttpContext.Response.RedirectPermanent("~/Account/FinishInfomation", true);
  76. return;
  77. }
  78. }
  79. IControlItemServices controlItemServices = scope.Resolve<IControlItemServices>();
  80. var menuNo = filterContext.HttpContext.Request["MNU"];
  81. var needControlMenu = controlItemServices.GetNeedControlMenuNo();
  82. if (needControlMenu.Any(x => x == menuNo))
  83. {
  84. try
  85. {
  86. controlItemServices.CheckControlItems(menuNo);
  87. }
  88. catch (Exception ex)
  89. {
  90. var currentRouteData = RouteTable.Routes.GetRouteData(filterContext.HttpContext);
  91. var currentController = "";
  92. var currentAction = "";
  93. if (currentRouteData != null)
  94. {
  95. if (currentRouteData.Values["controller"] != null &&
  96. !string.IsNullOrEmpty(currentRouteData.Values["controller"].ToString()))
  97. {
  98. currentController = currentRouteData.Values["controller"].ToString();
  99. }
  100. if (currentRouteData.Values["action"] != null &&
  101. !string.IsNullOrEmpty(currentRouteData.Values["action"].ToString()))
  102. {
  103. currentAction = currentRouteData.Values["action"].ToString();
  104. }
  105. }
  106. var controller = new CommonController();
  107. var action = "MsgShow";
  108. filterContext.HttpContext.ClearError();
  109. filterContext.HttpContext.Response.Clear();
  110. filterContext.HttpContext.Response.StatusCode = ex is HttpException ? ((HttpException)ex).GetHttpCode() : 500;
  111. filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
  112. controller.ViewData.Model = new HandleErrorInfo(ex, currentController, currentAction);
  113. filterContext.HttpContext.Response.Redirect("~/Common/MsgShow?url=" + filterContext.HttpContext.Server.UrlEncode("about:blank") + "&msg=" + filterContext.HttpContext.Server.UrlEncode(ex.Message), true);
  114. //((IController)controller).Execute(new RequestContext(filterContext.HttpContext, routeData));
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. }
  122. [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
  123. public class NoSSOAttribute : Attribute
  124. {
  125. }
  126. }