Global.asax.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System.Collections.Generic;
  2. using System.Data.Entity.Core.Mapping;
  3. using System.Data.Entity.Core.Metadata.Edm;
  4. using System.Data.Entity.Infrastructure;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Web;
  8. using System.Web.Mvc;
  9. using System.Web.Optimization;
  10. using System.Web.Routing;
  11. using Autofac;
  12. using Autofac.Integration.Mvc;
  13. using EMIS.Utility;
  14. using System.Resources;
  15. using EMIS.Web.Controllers;
  16. using System;
  17. using EMIS.Web.Controls.DataAnnotations;
  18. using Autofac.Integration.Wcf;
  19. using System.Globalization;
  20. using Bowin.Common;
  21. namespace EMIS.Web
  22. {
  23. // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明,
  24. // 请访问 http://go.microsoft.com/?LinkId=9394801
  25. public class MvcApplication : System.Web.HttpApplication, IAutofacContainerProvider
  26. {
  27. static IContainer _container;
  28. public IContainer Container
  29. {
  30. get { return _container; }
  31. set { _container = value; }
  32. }
  33. protected void Application_Start()
  34. {
  35. if (DateTimeFormatInfo.CurrentInfo != null)
  36. {
  37. var type = DateTimeFormatInfo.CurrentInfo.GetType();
  38. var field = type.GetField("generalLongTimePattern", BindingFlags.NonPublic | BindingFlags.Instance);
  39. if (field != null)
  40. field.SetValue(DateTimeFormatInfo.CurrentInfo, "yyyy-MM-dd HH:mm:ss");
  41. }
  42. var viewEngines = ViewEngines.Engines;
  43. var webFormEngines = viewEngines.OfType<WebFormViewEngine>().FirstOrDefault();
  44. if (webFormEngines != null)
  45. {
  46. viewEngines.Remove(webFormEngines);
  47. }
  48. AreaRegistration.RegisterAllAreas();
  49. BundleTable.EnableOptimizations = true;
  50. //WebApiConfig.Register(GlobalConfiguration.Configuration);
  51. FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
  52. RouteConfig.RegisterRoutes(RouteTable.Routes);
  53. BundleConfig.RegisterBundles(BundleTable.Bundles);
  54. //AuthConfig.RegisterAuth();
  55. var builder = new ContainerBuilder();
  56. builder.RegisterModule<EMIS.CommonLogic.DALModule>();
  57. builder.RegisterModule<EMIS.CommonLogic.ServiceModule>();
  58. builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
  59. try
  60. {
  61. var _container = builder.Build();
  62. AutofacHelper.Container = _container;
  63. AutofacHostFactory.Container = _container;
  64. DependencyResolver.SetResolver(new Autofac.Integration.Mvc.AutofacDependencyResolver(_container));
  65. }
  66. catch (Exception)
  67. {
  68. throw;
  69. }
  70. //EF优化代码
  71. using (Entities.EMISNewContext dbContext = new Entities.EMISNewContext())
  72. {
  73. var objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;
  74. var mappintCollection = (StorageMappingItemCollection)objectContext.MetadataWorkspace.GetItemCollection(DataSpace.CSSpace);
  75. mappintCollection.GenerateViews(new List<EdmSchemaError>());
  76. }
  77. //MVC视图验证映射
  78. ModelMetadataProviders.Current = new CustomerMetadataProvider();
  79. //ViewEngines.Engines.Clear();
  80. //ViewEngines.Engines.Add(new EcViewEngine());
  81. }
  82. //public sealed class EcViewEngine : RazorViewEngine
  83. //{
  84. // public EcViewEngine()
  85. // {
  86. // ViewLocationFormats = new[]
  87. // {
  88. // "~/Views/{1}/{0}.cshtml",//系统默认规则
  89. // "~/Views/Shared/{0}.cshtml",//系统默认规则
  90. // "~/Views/CalendarManage/{1}/{0}.cshtml",//我们的规则
  91. // "~/Views/EvaluationManage/{1}/{0}.cshtml",//我们的规则
  92. // };
  93. // }
  94. // public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
  95. // {
  96. // return base.FindView(controllerContext, viewName, masterName, useCache);
  97. // }
  98. //}
  99. ///// <summary>
  100. ///// 验证处理 IPrinceple
  101. ///// 将保存在票据中的信息转为当前的用户信息
  102. /////
  103. ///// </summary>
  104. ///// <param name="sender"></param>
  105. ///// <param name="e"></param>
  106. //protected void Application_AuthenticateRequest(Object sender, EventArgs e)
  107. //{
  108. // FormsAuthenticationHelper fahelper = new FormsAuthenticationHelper();
  109. // List<string> lurl = new List<string>();
  110. // lurl.Add("Content/");
  111. // lurl.Add("Doc/");
  112. // lurl.Add("Scripts/");
  113. // lurl.Add("Login/");
  114. // lurl.Add("UploadFile/");
  115. // var cookieName = EMIS.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME;
  116. // fahelper.AuthenticateRequest(Context, cookieName, lurl);
  117. //}
  118. void Application_BeginRequest(object sender, EventArgs e)
  119. {
  120. //黑名单内的地址必须验证
  121. if ((Const.LOCAL_AUTH_INCLUDEURL != null &&
  122. Const.LOCAL_AUTH_INCLUDEURL.Any(w => Request.Path.ToLower().IndexOf(w.ToLower()) >= 0)))
  123. {
  124. FormsAuthenticationHelper fahelper = new FormsAuthenticationHelper();
  125. var cookieName = EMIS.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME;
  126. fahelper.AuthenticateRequest(HttpContext.Current, cookieName, Const.LOCAL_AUTH_EXCEPTURL, Const.LOCAL_AUTH_INCLUDEURL);
  127. var userInfo = EMIS.Utility.FormValidate.CustomPrincipal.Current;
  128. if (userInfo == null)
  129. {
  130. throw new HttpException(401, "该资源需要登录后访问");
  131. }
  132. }
  133. }
  134. private void UpdateCookie(string cookie_name, string cookie_value)
  135. {
  136. HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name);
  137. if (null == cookie)
  138. {
  139. cookie = new HttpCookie(cookie_name);
  140. }
  141. cookie.Value = cookie_value;
  142. HttpContext.Current.Request.Cookies.Set(cookie);//重新设定请求中的cookie值,将服务器端的session值赋值给它
  143. }
  144. }
  145. }