县级监管平台

Global.asax.cs 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. 
  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. //Dictionary<string, string> Params = new Dictionary<string, string>();
  29. //Params.Add("request_url", context.Request.Url.ToString());
  30. //foreach (var key in context.Request.Params.AllKeys)
  31. //{
  32. // if (key == "ALL_HTTP")
  33. // {
  34. // break;
  35. // }
  36. // Params.Add(key, context.Request.Params[key]);
  37. //}
  38. bool issafe = true;
  39. foreach (var key in context.Request.Params.AllKeys)
  40. {
  41. if (key == "ALL_HTTP")
  42. {
  43. break;
  44. }
  45. try
  46. {
  47. string paras = context.Request.Params[key].ToString();
  48. if (!paras.Contains("data:image/png;base64,"))
  49. {
  50. Utils.SqlFilter(paras);
  51. }
  52. }
  53. catch
  54. {
  55. issafe = false;
  56. break;
  57. }
  58. }
  59. if (!issafe)
  60. {
  61. context.Response.Write(new AjaxResult { state = ResultTypes.error.ToString(), message = "非法输入!" }.ToJson());
  62. context.Response.End();
  63. }
  64. //var log = LogFactory.GetLogger(this.GetType().ToString());
  65. var token = context.Request["token"];
  66. if (string.IsNullOrWhiteSpace(token))
  67. {
  68. //log.Debug(Params.ToJson());
  69. return;
  70. }
  71. try
  72. {
  73. ////获取缓存
  74. //var dict = CacheHelper.Get(token);
  75. //获取redis缓存
  76. var dict = RedisHelper.StringGet(token);
  77. if (dict == null)
  78. {
  79. //log.Debug(Params.ToJson());
  80. return;
  81. }
  82. Dictionary<string, string> userData = null;
  83. //获取FormsAuthenticationTicket对象
  84. FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(token);
  85. if (ticket != null && string.IsNullOrEmpty(ticket.UserData) == false)
  86. userData = (new JavaScriptSerializer()).Deserialize<Dictionary<string, string>>(ticket.UserData); //还原用户数据
  87. if (ticket != null && userData != null)
  88. context.User = new FormsPrincipal<Dictionary<string, string>>(ticket, userData);//重新给context.User赋值。
  89. //var obj = new { UserCode = userData["F_UserCode"], Params = Params };
  90. //log.Debug(Params.ToJson());
  91. }
  92. catch {
  93. /* 有异常也不要抛出,防止攻击者试探。 */
  94. //log.Debug(Params.ToJson());
  95. }
  96. }
  97. }
  98. }