Sin descripción

Global.asax.cs 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using CallCenter.Utility;
  2. using CallCenterApi.Common;
  3. using CallCenterApi.Interface.Models.Common;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Web;
  8. using System.Web.Mvc;
  9. using System.Web.Routing;
  10. using System.Web.Script.Serialization;
  11. using System.Web.Security;
  12. namespace CallCenterApi.Interface
  13. {
  14. public class MvcApplication : System.Web.HttpApplication
  15. {
  16. protected void Application_Start()
  17. {
  18. AreaRegistration.RegisterAllAreas();
  19. RouteConfig.RegisterRoutes(RouteTable.Routes);
  20. //clq 增加异常日志记录 自定义 HandleErrorAttribute
  21. FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
  22. }
  23. protected void Application_AuthenticateRequest(object sender, EventArgs e)
  24. {
  25. HttpApplication app = (HttpApplication)sender;
  26. var context = app.Context;
  27. if (context == null) throw new ArgumentNullException("context");
  28. try
  29. {
  30. var date = DateTime.Parse(Configs.GetValue("AuthDate"));
  31. if (date < DateTime.Now)
  32. {
  33. context.Response.ContentType = "text/plain; charset=utf-8";
  34. context.Response.Write(new AjaxResult { state = ResultTypes.error.ToString(), message = "授权过期,请联系系统厂家。" }.ToJson());
  35. context.Response.End();
  36. }
  37. }
  38. catch (Exception ex)
  39. {
  40. var log = LogFactory.GetLogger(this.GetType().ToString());
  41. log.Error(Configs.GetValue("AuthDate"));
  42. log.Error(ex.ToString());
  43. }
  44. var token = context.Request["token"];
  45. if (string.IsNullOrWhiteSpace(token)) return;
  46. try
  47. {
  48. //获取缓存
  49. var dict = CacheHelper.Get(token);
  50. if (dict == null) return;
  51. Cache.Models.CurrentUserInfo userData = null;
  52. //获取FormsAuthenticationTicket对象
  53. FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(token);
  54. if (ticket != null && string.IsNullOrEmpty(ticket.UserData) == false)
  55. userData = (new JavaScriptSerializer()).Deserialize<Cache.Models.CurrentUserInfo>(ticket.UserData); //还原用户数据
  56. if (ticket != null && userData != null)
  57. context.User = new FormsPrincipal<Cache.Models.CurrentUserInfo> (ticket, userData);//重新给context.User赋值。
  58. }
  59. catch { /* 有异常也不要抛出,防止攻击者试探。 */ }
  60. }
  61. }
  62. }