Authorization.cs 7.5 KB

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