ASP.Net - unvalidated request exceptions?

0

Environment:

Windows 2008 R2 Server
IIS 7.5
ASP.Net 4.5.1
web.config <httpRuntime requestValidationMode="2.0" />

I have a function that is used to return an Integer ID from the querystring, form or cookies collection. 99% of the time it works just fine, below is the code being used:

Function get_id() as Integer

Const k_fld_name as String = "id"

Dim ok As Boolean

Dim id as Integer

Dim fld_val As String

Dim cookie as HttpCookie

Dim req As System.Web.HttpRequest


' Attempt to get ID ...

id = 0

fld_val = Nothing


' Get current request (if any) ...
' NOTE: https://msdn.microsoft.com/en-us/library/system.web.httpcontext.request(v=vs.110).aspx
' ASP.NET will throw an exception if you try to use this property when the HttpRequest object 
' Is Not available. For example, this would be true in the Application_Start method of the Global.asax file, 
' Or in a method that Is called from the Application_Start method. At that time no HTTP request 
' has been created yet ...

Try

   req = System.Web.HttpContext.Current.Request

Catch

   req = Nothing

End Try

If ( req Is Nothing ) Then

   Return id

End If


Try

'  Check in querystring ...

   fld_val = req.Unvalidated.QueryString( k_fld_name )

'  If not found, check in form ...

   If ( String.IsNullOrEmpty( fld_val ) ) Then

      fld_val = req.Unvalidated.Form( k_fld_name )

'     If not found, check in cookie ...

      If ( String.IsNullOrEmpty( fld_val ) ) Then

         cookie = req.Unvalidated.Cookies( k_fld_name )

         If ( cookie isNot Nothing ) Then

            fld_val = cookie.Value

         End If

      End If

   End If

Catch ex As Exception

   error_log( ex.ToString() )

End Try

[ other logic to convert to Integer ]

Return id

End Function

But every so often, the following exception is being logged:

System.Web.HttpException (0x80004005): An error occurred while communicating with the remote host. The error code is 0x800703E3. ---> System.NotSupportedException: Specified method is not supported.
   at System.Web.HttpResponseStream.get_Position()
   at System.Drawing.UnsafeNativeMethods.ComStreamFromDataStream.Seek(Int64 offset, Int32 origin)
   at System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 result, Boolean throwOnDisconnect)
   at System.Web.Hosting.IIS7WorkerRequest.ReadEntityCoreSync(Byte[] buffer, Int32 offset, Int32 size)
   at System.Web.Hosting.IIS7WorkerRequest.ReadEntityBody(Byte[] buffer, Int32 size)
   at System.Web.HttpRequest.GetEntireRawContent()
   at System.Web.HttpRequest.FillInFormCollection()
   at System.Web.HttpRequest.EnsureForm()
   at System.Web.UnvalidatedRequestValues.get_Form()

Why System.Drawing is showing up is simply beyond me. I would like to know WHAT method is not supported as per the exception message?

Do I need to check if the .Unvalidated property is not null as well as any other collections, ie:

If ( req.Unvalidated isNot Nothing ) Then
   ...

   If ( req.Unvalidated.Form isNot Nothing ) Then

     ...

   End If

End If

Unfortunately, the MS documentation does not state whether or not the above properties are always present (have a value) or not (are null).

Any suggestions or ideas would be appreciated as I am against a brick wall on this one.

Thanks in advance.

asp.net
exception
request
asked on Stack Overflow Aug 18, 2016 by bdcoder

1 Answer

0

Put ValidateRequest="false" in your aspx page

<% ValidateRequest="false"%> 

then use in your code file

Request.Unvalidated.Form["yourrequest"];
answered on Stack Overflow Mar 26, 2017 by Muhammad Rehan Baig • edited Mar 26, 2017 by pacholik

User contributions licensed under CC BY-SA 3.0