123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- using System.Collections.Generic;
- using System.Data.Entity.Core.Mapping;
- using System.Data.Entity.Core.Metadata.Edm;
- using System.Data.Entity.Infrastructure;
- using System.Linq;
- using System.Reflection;
- using System.Web;
- using System.Web.Mvc;
- using System.Web.Optimization;
- using System.Web.Routing;
- using Autofac;
- using Autofac.Integration.Mvc;
- using EMIS.Utility;
- using System.Resources;
- using EMIS.Web.Controllers;
- using System;
- using EMIS.Web.Controls.DataAnnotations;
- using Autofac.Integration.Wcf;
- using System.Globalization;
- using Bowin.Common;
- namespace EMIS.Web
- {
- // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明,
- // 请访问 http://go.microsoft.com/?LinkId=9394801
- public class MvcApplication : System.Web.HttpApplication, IAutofacContainerProvider
- {
- static IContainer _container;
- public IContainer Container
- {
- get { return _container; }
- set { _container = value; }
- }
- protected void Application_Start()
- {
- if (DateTimeFormatInfo.CurrentInfo != null)
- {
- var type = DateTimeFormatInfo.CurrentInfo.GetType();
- var field = type.GetField("generalLongTimePattern", BindingFlags.NonPublic | BindingFlags.Instance);
- if (field != null)
- field.SetValue(DateTimeFormatInfo.CurrentInfo, "yyyy-MM-dd HH:mm:ss");
- }
- var viewEngines = ViewEngines.Engines;
- var webFormEngines = viewEngines.OfType<WebFormViewEngine>().FirstOrDefault();
- if (webFormEngines != null)
- {
- viewEngines.Remove(webFormEngines);
- }
- AreaRegistration.RegisterAllAreas();
- BundleTable.EnableOptimizations = true;
- //WebApiConfig.Register(GlobalConfiguration.Configuration);
- FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
- RouteConfig.RegisterRoutes(RouteTable.Routes);
- BundleConfig.RegisterBundles(BundleTable.Bundles);
- //AuthConfig.RegisterAuth();
- var builder = new ContainerBuilder();
- builder.RegisterModule<EMIS.CommonLogic.DALModule>();
- builder.RegisterModule<EMIS.CommonLogic.ServiceModule>();
- builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
- try
- {
- var _container = builder.Build();
- AutofacHelper.Container = _container;
- AutofacHostFactory.Container = _container;
- DependencyResolver.SetResolver(new Autofac.Integration.Mvc.AutofacDependencyResolver(_container));
- }
- catch (Exception)
- {
-
- throw;
- }
- //EF优化代码
- using (Entities.EMISNewContext dbContext = new Entities.EMISNewContext())
- {
- var objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;
- var mappintCollection = (StorageMappingItemCollection)objectContext.MetadataWorkspace.GetItemCollection(DataSpace.CSSpace);
- mappintCollection.GenerateViews(new List<EdmSchemaError>());
- }
- //MVC视图验证映射
- ModelMetadataProviders.Current = new CustomerMetadataProvider();
- //ViewEngines.Engines.Clear();
- //ViewEngines.Engines.Add(new EcViewEngine());
- }
- //public sealed class EcViewEngine : RazorViewEngine
- //{
- // public EcViewEngine()
- // {
- // ViewLocationFormats = new[]
- // {
- // "~/Views/{1}/{0}.cshtml",//系统默认规则
- // "~/Views/Shared/{0}.cshtml",//系统默认规则
- // "~/Views/CalendarManage/{1}/{0}.cshtml",//我们的规则
- // "~/Views/EvaluationManage/{1}/{0}.cshtml",//我们的规则
- // };
- // }
- // public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
- // {
- // return base.FindView(controllerContext, viewName, masterName, useCache);
- // }
- //}
- ///// <summary>
- ///// 验证处理 IPrinceple
- ///// 将保存在票据中的信息转为当前的用户信息
- /////
- ///// </summary>
- ///// <param name="sender"></param>
- ///// <param name="e"></param>
- //protected void Application_AuthenticateRequest(Object sender, EventArgs e)
- //{
- // FormsAuthenticationHelper fahelper = new FormsAuthenticationHelper();
- // List<string> lurl = new List<string>();
- // lurl.Add("Content/");
- // lurl.Add("Doc/");
- // lurl.Add("Scripts/");
- // lurl.Add("Login/");
- // lurl.Add("UploadFile/");
- // var cookieName = EMIS.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME;
- // fahelper.AuthenticateRequest(Context, cookieName, lurl);
- //}
- void Application_BeginRequest(object sender, EventArgs e)
- {
- //黑名单内的地址必须验证
- if ((Const.LOCAL_AUTH_INCLUDEURL != null &&
- Const.LOCAL_AUTH_INCLUDEURL.Any(w => Request.Path.ToLower().IndexOf(w.ToLower()) >= 0)))
- {
- FormsAuthenticationHelper fahelper = new FormsAuthenticationHelper();
- var cookieName = EMIS.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME;
- fahelper.AuthenticateRequest(HttpContext.Current, cookieName, Const.LOCAL_AUTH_EXCEPTURL, Const.LOCAL_AUTH_INCLUDEURL);
- var userInfo = EMIS.Utility.FormValidate.CustomPrincipal.Current;
- if (userInfo == null)
- {
- throw new HttpException(401, "该资源需要登录后访问");
- }
- }
- }
- private void UpdateCookie(string cookie_name, string cookie_value)
- {
- HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name);
- if (null == cookie)
- {
- cookie = new HttpCookie(cookie_name);
- }
- cookie.Value = cookie_value;
- HttpContext.Current.Request.Cookies.Set(cookie);//重新设定请求中的cookie值,将服务器端的session值赋值给它
- }
- }
- }
|