Created
January 20, 2016 14:09
-
-
Save lennybacon/c76ef2b927ef1e4d6cae to your computer and use it in GitHub Desktop.
Reading IIS request filtering maximal request length
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.IO; | |
| using System.Linq; | |
| using System.Web; | |
| using System.Web.Configuration; | |
| using System.Xml; | |
| namespace lennybacon.Samples | |
| { | |
| class IisMaxRequestLength | |
| { | |
| private static int? GetMaxRequestLengthOfRequestFiltering() | |
| { | |
| const int requestFilteringDefaultLimit = 30000000; | |
| var configuration = WebConfigurationManager.OpenWebConfiguration("~"); | |
| var webServerSection = | |
| configuration.GetSection("system.webServer"); | |
| var xml = webServerSection.SectionInformation.GetRawXml(); | |
| var dom = new XmlDocument(); | |
| dom.LoadXml(xml); | |
| var requestLimits = | |
| dom.SelectSingleNode( | |
| "/system.webServer/security/requestFiltering/requestLimits"); | |
| var requestFilteringInstalled = false; | |
| if (requestLimits == null) | |
| { | |
| var appPoolName = | |
| HttpContext.Current.Request.ServerVariables["APP_POOL_ID"]; | |
| var windowsDrive = | |
| new DirectoryInfo( | |
| Environment.GetFolderPath(Environment.SpecialFolder.System)).Root; | |
| var appPoolTempCfgPath = | |
| Path.Combine( | |
| windowsDrive.FullName, | |
| "inetpub", | |
| "temp", | |
| "appPools", | |
| appPoolName, | |
| appPoolName + ".config"); | |
| dom = new XmlDocument(); | |
| dom.Load(appPoolTempCfgPath); | |
| requestFilteringInstalled = | |
| dom.SelectSingleNode( | |
| "//system.webServer/security/requestFiltering") == null; | |
| requestLimits = | |
| dom.SelectSingleNode( | |
| "//system.webServer/security/requestFiltering/requestLimits"); | |
| } | |
| if (requestLimits != null) | |
| { | |
| const string maxAllowedContentLengthName = "maxAllowedContentLength"; | |
| var attribute = | |
| requestLimits.Attributes?.Cast<XmlAttribute>() | |
| .FirstOrDefault( | |
| a => | |
| maxAllowedContentLengthName.Equals( | |
| a.Name, | |
| StringComparison.Ordinal) | |
| ); | |
| if (attribute != null) | |
| { | |
| var requestFilteringLimit = 0; | |
| if (int.TryParse(attribute.Value, out requestFilteringLimit)) | |
| { | |
| return requestFilteringLimit; | |
| } | |
| } | |
| } | |
| if (!requestFilteringInstalled) | |
| { | |
| return null; | |
| } | |
| return requestFilteringDefaultLimit; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment