Authorization.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.IsStudent)
  51. {
  52. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  53. {
  54. if (!filterContext.HttpContext.Request.Path.Contains("/Account/FinishInfomation")
  55. && !filterContext.HttpContext.Request.Path.Contains("/Home/Index")
  56. && !filterContext.HttpContext.Request.Path.Contains("/Common/")
  57. && !filterContext.HttpContext.Request.Path.Contains("/GetUserRoleDropdownList")
  58. && filterContext.HttpContext.Request.Path != "/"
  59. && !filterContext.HttpContext.Request.Path.Contains("/Account/LogOff"))
  60. {
  61. IStudentsServices studentsServices = scope.Resolve<IStudentsServices>();
  62. if (!studentsServices.CheckNecessary(userInfo.UserID))
  63. {
  64. filterContext.HttpContext.Response.RedirectPermanent("~/Account/FinishInfomation", true);
  65. return;
  66. }
  67. }
  68. IControlItemServices controlItemServices = scope.Resolve<IControlItemServices>();
  69. var menuNo = filterContext.HttpContext.Request["MNU"];
  70. var needControlMenu = controlItemServices.GetNeedControlMenuNo();
  71. if (needControlMenu.Any(x => x == menuNo))
  72. {
  73. try
  74. {
  75. controlItemServices.CheckControlItems(menuNo);
  76. }
  77. catch (Exception ex)
  78. {
  79. var currentRouteData = RouteTable.Routes.GetRouteData(filterContext.HttpContext);
  80. var currentController = "";
  81. var currentAction = "";
  82. if (currentRouteData != null)
  83. {
  84. if (currentRouteData.Values["controller"] != null &&
  85. !string.IsNullOrEmpty(currentRouteData.Values["controller"].ToString()))
  86. {
  87. currentController = currentRouteData.Values["controller"].ToString();
  88. }
  89. if (currentRouteData.Values["action"] != null &&
  90. !string.IsNullOrEmpty(currentRouteData.Values["action"].ToString()))
  91. {
  92. currentAction = currentRouteData.Values["action"].ToString();
  93. }
  94. }
  95. var controller = new CommonController();
  96. var action = "MsgShow";
  97. filterContext.HttpContext.ClearError();
  98. filterContext.HttpContext.Response.Clear();
  99. filterContext.HttpContext.Response.StatusCode = ex is HttpException ? ((HttpException)ex).GetHttpCode() : 500;
  100. filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
  101. controller.ViewData.Model = new HandleErrorInfo(ex, currentController, currentAction);
  102. filterContext.HttpContext.Response.Redirect("~/Common/MsgShow?url=" + filterContext.HttpContext.Server.UrlEncode("about:blank") + "&msg=" + filterContext.HttpContext.Server.UrlEncode(ex.Message), true);
  103. //((IController)controller).Execute(new RequestContext(filterContext.HttpContext, routeData));
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }
  111. [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
  112. public class NoSSOAttribute : Attribute
  113. {
  114. }
  115. }