| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using CallCenter.Utility;
- using CallCenterApi.Common;
- using CallCenterApi.Interface.Models.Common;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.Web.Routing;
- using System.Web.Script.Serialization;
- using System.Web.Security;
- namespace CallCenterApi.Interface
- {
- public class MvcApplication : System.Web.HttpApplication
- {
- protected void Application_Start()
- {
- AreaRegistration.RegisterAllAreas();
- RouteConfig.RegisterRoutes(RouteTable.Routes);
- //clq 增加异常日志记录 自定义 HandleErrorAttribute
- FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
- }
- protected void Application_AuthenticateRequest(object sender, EventArgs e)
- {
- HttpApplication app = (HttpApplication)sender;
- var context = app.Context;
- if (context == null) throw new ArgumentNullException("context");
- try
- {
- var date = DateTime.Parse(Configs.GetValue("AuthDate"));
- if (date < DateTime.Now)
- {
- context.Response.ContentType = "text/plain; charset=utf-8";
- context.Response.Write(new AjaxResult { state = ResultTypes.error.ToString(), message = "授权过期,请联系系统厂家。" }.ToJson());
- context.Response.End();
- }
- }
- catch (Exception ex)
- {
- var log = LogFactory.GetLogger(this.GetType().ToString());
- log.Error(Configs.GetValue("AuthDate"));
- log.Error(ex.ToString());
- }
- var token = context.Request["token"];
- if (string.IsNullOrWhiteSpace(token)) return;
- try
- {
- //获取缓存
- var dict = CacheHelper.Get(token);
- if (dict == null) return;
- Cache.Models.CurrentUserInfo userData = null;
- //获取FormsAuthenticationTicket对象
- FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(token);
- if (ticket != null && string.IsNullOrEmpty(ticket.UserData) == false)
- userData = (new JavaScriptSerializer()).Deserialize<Cache.Models.CurrentUserInfo>(ticket.UserData); //还原用户数据
- if (ticket != null && userData != null)
- context.User = new FormsPrincipal<Cache.Models.CurrentUserInfo> (ticket, userData);//重新给context.User赋值。
- }
- catch { /* 有异常也不要抛出,防止攻击者试探。 */ }
- }
- }
- }
|