private void Application_Start(object sender, EventArgs e) { ScriptingJsonSerializationSection section; string item = ConfigurationManager.AppSettings["maxSessionsAllowedPerUser"]; if (item != null) { int num = 0; if (!int.TryParse(item, NumberStyles.Integer, CultureInfo.InvariantCulture, out num)) { PowwaEvents.PowwaEVENT_INVALID_APPLICATION_SETTING("maxSessionsAllowedPerUser", item); } else { PowwaAuthorizationManager.Instance.UserSessionsLimit = num; } } try { Configuration configuration = WebConfigurationManager.OpenWebConfiguration("/"); section = (ScriptingJsonSerializationSection)configuration.GetSection("system.web.extensions/scripting/webServices/jsonSerialization"); } catch { section = new ScriptingJsonSerializationSection(); } PowwaSessionManager.Instance.JsonSerializer.MaxJsonLength = section.MaxJsonLength; }
private static JavaScriptConverter[] CreateConfigConverters() { List <JavaScriptConverter> list = new List <JavaScriptConverter>(); ScriptingJsonSerializationSection section = WebConfigFactory.GetJsonSerializationSection(); if (section != null) { foreach (Converter converter in section.Converters) { Type c = System.Web.Compilation.BuildManager.GetType(converter.Type, false); if (c == null) { throw new ArgumentException("类型不能为空"); } if (!typeof(JavaScriptConverter).IsAssignableFrom(c)) { throw new ArgumentException("类型没有继承自JavaScriptConverter"); } list.Add((JavaScriptConverter)TypeCreator.CreateInstance(c)); } } return(list.ToArray()); }
static JsonHelper() { ScriptingJsonSerializationSection settings = (ScriptingJsonSerializationSection)ConfigurationManager.GetSection("system.web.extensions/scripting/webServices/jsonSerialization"); jsonSerializer.MaxJsonLength = settings.MaxJsonLength; jsonSerializer.RecursionLimit = settings.RecursionLimit; jsonSerializer.RegisterConverters(CreateConverters(settings.Converters)); }
protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior) { ScriptingJsonSerializationSection configSection = JavaScriptSerializerExtensions.GetScriptingJsonSerializationSection(); return(new JsonResultWithMaxJsonLimits { Data = data, ContentType = contentType, ContentEncoding = contentEncoding, JsonRequestBehavior = behavior, MaxJsonLength = configSection.MaxJsonLength, RecursionLimit = configSection.RecursionLimit }); }
public void ExceedMaxJsonLengthSerializationLengthTest() { ScriptingJsonSerializationSection scriptSection = (ScriptingJsonSerializationSection)ConfigurationBroker.GetSection("jsonSerialization"); Console.WriteLine(scriptSection.MaxJsonLength); string data = PrepateTestStringByLength(15000); string json = JSONSerializerExecute.Serialize(data); }
protected void Page_Load(object sender, EventArgs e) { JavaScriptSerializer serializer = new JavaScriptSerializer(); ScriptingJsonSerializationSection section = ConfigurationManager.GetSection("system.web.extensions/scripting/webServices/jsonSerialization") as ScriptingJsonSerializationSection; if (section != null) { serializer.MaxJsonLength = section.MaxJsonLength; serializer.RecursionLimit = section.RecursionLimit; } }
/// <summary> /// 获取JsonSerializationSection /// </summary> /// <returns>JsonSerializationSection</returns> /// <remarks>获取JsonSerializationSection</remarks> public static ScriptingJsonSerializationSection GetJsonSerializationSection() { ScriptingJsonSerializationSection section = (ScriptingJsonSerializationSection)ConfigurationBroker.GetSection("lib.web/jsonConverter"); if (section == null) { section = new ScriptingJsonSerializationSection(); } return(section); }
public static JavaScriptSerializer CreateJavaScriptSerializer() { // Create Serializer and initialize it with the right settings ScriptingJsonSerializationSection configSection = GetScriptingJsonSerializationSection(); JavaScriptSerializer javascriptSerializer = new JavaScriptSerializer(); javascriptSerializer.MaxJsonLength = configSection.MaxJsonLength; javascriptSerializer.RecursionLimit = configSection.RecursionLimit; return(javascriptSerializer); }
/// <summary> /// 获取JsonSerializationSection /// </summary> /// <returns>JsonSerializationSection</returns> /// <remarks>获取JsonSerializationSection</remarks> private static ScriptingJsonSerializationSection GetJsonSerializationSection() { ScriptingJsonSerializationSection section = (ScriptingJsonSerializationSection)ConfigurationBroker.GetSection("scriptJsonSerialization"); if (section == null) { section = new ScriptingJsonSerializationSection(); } return(section); }
protected void Application_BeginRequest(Object sender, EventArgs e) { if (string.IsNullOrEmpty(applicationPath)) { applicationPath = Request.ApplicationPath; initRequestUrl = Request.Url.ToString(); //Logging(Request, null, initRequestUrl, null, "Application Started", GetRequestInfo()); try { string str = HttpContext.Current.Request.ApplicationPath.ToString(); Configuration conf = WebConfigurationManager.OpenWebConfiguration(str); ScriptingJsonSerializationSection section = (ScriptingJsonSerializationSection)conf.GetSection("system.web.extensions/scripting/webServices/jsonSerialization"); Application["MaxJsonLength"] = section.MaxJsonLength.ToString(); } catch { } started = true; } if (!pulseStarted //&& !HttpContext.Current.Request.Url.AbsolutePath.Contains("/CronJob.aspx") ) { StartBackgroundTask(Config.EnableSsl); } string extBasePath = Config.ExtBasePath ?? ""; string appPath = Request.ApplicationPath; if (Request.Path == appPath && appPath.ToLower() != extBasePath.ToLower() && Request.ApplicationPath != "/" && Request.Headers["X-Forwarded-For"] != null ) { string redirectUrl = Request.Path + "/"; try { Dictionary <string, string> requestHeader = new Dictionary <string, string>(); foreach (string x in Request.Headers.Keys) { requestHeader[x] = Request.Headers[x]; } requestHeader["Host"] = Request.Url.Host; requestHeader["ApplicationPath"] = appPath; string url = Utils.transformProxyUrl(redirectUrl, requestHeader); Response.Redirect(url); } catch { } } }
/// <summary> /// 对象序列化 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static string objectToJson(object obj) { JavaScriptSerializer jss = new JavaScriptSerializer(); ScriptingJsonSerializationSection section = WebConfigurationManager.GetSection("system.web.extensions/scripting/webServices/jsonSerialization") as ScriptingJsonSerializationSection; if (section != null) { jss.MaxJsonLength = section.MaxJsonLength; jss.RecursionLimit = section.RecursionLimit; } return(jss.Serialize(obj)); }
internal JavaScriptSerializer(JavaScriptTypeResolver resolver, bool registerConverters) { _typeResolver = resolver; ScriptingJsonSerializationSection section = (ScriptingJsonSerializationSection)ConfigurationManager.GetSection("system.web.extensions/scripting/webServices/jsonSerialization"); if (section == null) { #if NET_3_5 _maxJsonLength = 2097152; #else _maxJsonLength = 102400; #endif _recursionLimit = 100; } else { _maxJsonLength = section.MaxJsonLength; _recursionLimit = section.RecursionLimit; if (registerConverters) { ConvertersCollection converters = section.Converters; if (converters != null && converters.Count > 0) { var cvtlist = new List <JavaScriptConverter> (); Type type; string typeName; JavaScriptConverter jsc; foreach (Converter cvt in converters) { typeName = cvt != null ? cvt.Type : null; if (typeName == null) { continue; } type = HttpApplication.LoadType(typeName, true); if (type == null || !typeof(JavaScriptConverter).IsAssignableFrom(type)) { continue; } jsc = Activator.CreateInstance(type) as JavaScriptConverter; cvtlist.Add(jsc); } RegisterConverters(cvtlist); } } } }
public static ScriptingJsonSerializationSection GetJsonSerializationSection() { //原来的配置节的名称为jsonSerialization,现在更换为scriptJsonSerialization ScriptingJsonSerializationSection section = (ScriptingJsonSerializationSection)ConfigurationBroker.GetSection("deluxe.web/scriptJsonSerialization"); if (section == null) { section = new ScriptingJsonSerializationSection(); } return(section); }
/// <summary> /// Zwraca maksymalną wielkość obiektu JSON w bajtach /// </summary> public static int GetJsonMaxLength() { var result = 2097152; try { ///Próba odczytania parametru z pliku konfiguracyjnego var conf = WebConfigurationManager.OpenWebConfiguration("~"); ScriptingJsonSerializationSection jsonSection = conf.GetSection("system.web.extensions/scripting/webServices/jsonSerialization") as ScriptingJsonSerializationSection; result = jsonSection.MaxJsonLength; } catch (Exception) { } return(result); }
/// <summary> /// 返回json数据格式 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static HttpResponseMessage GetJsonData(object obj) { JavaScriptSerializer serializer = new JavaScriptSerializer(); ScriptingJsonSerializationSection section = ConfigurationManager.GetSection("system.web.extensions/scripting/webServices/jsonSerialization") as ScriptingJsonSerializationSection; if (section != null) { serializer.MaxJsonLength = section.MaxJsonLength; serializer.RecursionLimit = section.RecursionLimit; } string str = serializer.Serialize(obj); HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(str, System.Text.Encoding.GetEncoding("UTF-8"), "application/json") }; return(result); }
public static ScriptingJsonSerializationSection GetScriptingJsonSerializationSection() { if (scriptJsonSerializationSection == null) { lock (syncLock) { if (scriptJsonSerializationSection == null) { System.Configuration.Configuration webConfiguration = WebConfigurationManager.OpenWebConfiguration("/"); // Get the object related to the <system.web.extensions> section scriptJsonSerializationSection = (ScriptingJsonSerializationSection)webConfiguration.GetSection("system.web.extensions/scripting/webServices/jsonSerialization"); } } } return(scriptJsonSerializationSection); }
public override void ExecuteResult(ControllerContext context) { if (context == null) { throw new ArgumentNullException("context"); } if (JsonRequestBehavior == JsonRequestBehavior.DenyGet && String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) { throw new InvalidOperationException("This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet."); } HttpResponseBase response = context.HttpContext.Response; if (!String.IsNullOrEmpty(ContentType)) { response.ContentType = ContentType; } else { response.ContentType = "application/json"; } if (ContentEncoding != null) { response.ContentEncoding = ContentEncoding; } if (Data != null) { JavaScriptSerializer serializer = new JavaScriptSerializer(); ScriptingJsonSerializationSection section = ConfigurationManager.GetSection("system.web.extensions/scripting/webServices/jsonSerialization") as ScriptingJsonSerializationSection; if (section != null) { serializer.MaxJsonLength = section.MaxJsonLength; serializer.RecursionLimit = section.RecursionLimit; } try { response.Write(serializer.Serialize(Data)); } catch (Exception) { response.Write("error"); } } }
private static List <JavaScriptConverter> GetGlobalConvertersInConfig() { List <JavaScriptConverter> list = S_GlobalConvertersInConfig; lock (S_GlobalConvertersInConfig) { ScriptingJsonSerializationSection section = GetJsonSerializationSection(); if (S_GlobalConverterSection != section) { list.Clear(); foreach (Converter converter in section.Converters) { Type c = System.Web.Compilation.BuildManager.GetType(converter.Type, false); if (c == null) { //沈峥调整,不抛出异常 //throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, Resources.DeluxeWebResource.E_UnknownType, new object[] { converter.Type })); System.Exception ex = new ArgumentException(string.Format(CultureInfo.InvariantCulture, Resources.DeluxeJsonResource.E_UnknownType, new object[] { converter.Type })); ExceptionHelper.WriteToEventLog(ex, "JavaScriptConverter", EventLogEntryType.Warning); } else if (!typeof(JavaScriptConverter).IsAssignableFrom(c)) { throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, Resources.DeluxeJsonResource.E_NotJavaScriptConverter, new object[] { c.Name })); } if (c != null) { list.Add((JavaScriptConverter)TypeCreator.CreateInstance(c)); } } S_GlobalConverterSection = section; } return(list); } }
public static void GetConverterElement() { // Get the Web application configuration. System.Configuration.Configuration configuration = WebConfigurationManager.OpenWebConfiguration("/aspnetTest"); // Get the external JSON section. ScriptingJsonSerializationSection jsonSection = (ScriptingJsonSerializationSection)configuration.GetSection( "system.web.extensions/scripting/webServices/jsonSerialization"); //Get the converters collection. ConvertersCollection converters = jsonSection.Converters; if ((converters != null) && converters.Count > 0) { // Get the first registered converter. Converter converterElement = converters[0]; } }
/// <summary> /// 从配置文件中读取配置的最大Json尺寸 /// </summary> /// <returns></returns> public static int GetMaxJsonLength() { int result = -1; ScriptingJsonSerializationSection scriptSection = (ScriptingJsonSerializationSection)ConfigurationBroker.GetSection("jsonSerialization"); if (scriptSection == null) { scriptSection = (ScriptingJsonSerializationSection)ConfigurationBroker.GetSection("scriptJsonSerialization"); } if (scriptSection != null) { result = scriptSection.MaxJsonLength; } if (result < 0) { result = int.MaxValue; } return(result); }
protected void Application_BeginRequest(Object sender, EventArgs e) { if (string.IsNullOrEmpty(applicationPath)) { applicationPath = Request.ApplicationPath; initRequestUrl = Request.Url.ToString(); //Logging(Request, null, initRequestUrl, null, "Application Started", GetRequestInfo()); try { string str = HttpContext.Current.Request.ApplicationPath.ToString(); Configuration conf = WebConfigurationManager.OpenWebConfiguration(str); ScriptingJsonSerializationSection section = (ScriptingJsonSerializationSection)conf.GetSection("system.web.extensions/scripting/webServices/jsonSerialization"); Application["MaxJsonLength"] = section.MaxJsonLength.ToString(); } catch { } started = true; } if (!pulseStarted //&& !HttpContext.Current.Request.Url.AbsolutePath.Contains("/CronJob.aspx") ) { StartBackgroundTask(Config.EnableSsl); } string extBasePath = Config.ExtBasePath ?? ""; string appPath = Request.ApplicationPath; if (Request.Path == appPath && appPath.ToLower() != extBasePath.ToLower() && Request.ApplicationPath != "/" && Request.Headers["X-Forwarded-For"] != null ) { string redirectUrl = Request.Path + "/"; try { Dictionary <string, string> requestHeader = new Dictionary <string, string>(); foreach (string x in Request.Headers.Keys) { requestHeader[x] = Request.Headers[x]; } requestHeader["Host"] = Request.Url.Host; requestHeader["ApplicationPath"] = appPath; string url = Utils.transformProxyUrl(redirectUrl, requestHeader); Response.Redirect(url); } catch { } } else { // IIS restriction, modifying cache-control invalidate default document handling in classic pipeline(so must switch to integrated if this is required) if (Config.PageCacheControl > 0) { // must deliberately set this for time based caching or it becomes public Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate); // no-cache, max-age and expiry has no effect on forward/backward button(and most likely history) // chrome/firefox default is no-cache if there is no cache-control, except forward/backward button Response.Cache.SetExpires(DateTime.UtcNow.AddSeconds(Config.PageCacheControl.Value)); } else if (Config.PageCacheControl == 0) { // deliberately want to retain default asp.net behavior(say to use classic pipeline // won't do anything to the cache control pipeline } else // extreme no-cache { //this is absolutely no caching, effectively same as 'form post', even back/forward button in chrome would hit server Response.Cache.SetNoStore(); } } }