4

I've specified a maxRequestLength in my web.config (MVC) like so:

  <location path="File/Upload">
    <system.web>
      <httpRuntime maxRequestLength="330"/>
    </system.web>
  </location>

When having a look in the network tab while testing for file uploads that exceed 330kb, I can see the response from the server is 500 and the details are The required anti-forgery form field &quot;__RequestVerificationToken&quot; is not present.

Why is this the case? Is the framework not capable of providing details of maxRequestLength in the response? Or is it that it merely doesn't want to in order to not give away info about the system?

Or is it that my request has been truncated/cropped in order to cater for the limit, which has inadvertently trimmed off the request verification token?

NOTE

This works fine with files that do not exceed 330kb, and my upload works.

  • It's my understanding that you should see the max length error message. It is possible that you are having multiple errors and this one is returning before you even hit the max length, see the link to fix your current error, hopefully that uncovers the one you are looking for http://stackoverflow.com/questions/16102957/the-required-anti-forgery-form-field-requestverificationtoken-is-not-present – aemorales1 Feb 18 '16 at 16:18
  • This works fine with a file of acceptable size @aemorales1. Thanks for your reply. –  Feb 19 '16 at 19:52
  • Are you using the `[ValidateAntiForgeryToken]` on any of your Actions? Could you be seeing the result of a redirect to one of those Actions? – DigitalDan Feb 22 '16 at 12:17

3 Answers3

0

I see you set maxRequestLength is 330, this is the limit for file upload. this value follow the rule below:

1 Mb = 1024kb

as you can see more detail at:

https://msdn.microsoft.com/en-us/library/system.web.configuration.httpruntimesection.maxrequestlength(v=vs.110).aspx

So when you set maxRequestLength="330", the system will be understand that the file upload is limit 330kb.

Binh LE
  • 377
  • 5
  • 17
0

I suspect one of your Actions (not necessarily the one that handles the File Upload) has the [ValidateAntiForgeryToken] attribute, and that you're somehow seeing the error coming from that Action instead.

I suggest temporarily commenting out all the [ValidateAntiForgeryToken] attributes in your project to see what's actually being returned when you try to upload files that are over the limit.

DigitalDan
  • 2,042
  • 2
  • 24
  • 28
0

I tried recreating the issue on a sample project but upon further research I found out that ASP.NET MVC controller actions do not use the location element in the web.config file. This is because unlike ASP.NET which maps file to the disk, ASP.NET MVC uses routing. You can check out the answer on the link below which although relates to a different question, notes that the location tag isn't used.

How do I allow all users access to one route within a website with integrated auth?

In this case the framework is most likely not able to enforce the restriction on the file size for Upload action.

Am not entirely sure since am unable to recreate the 500 Internal Server Error but I think it could be as a result of the framework failing to correctly interpret your web.config due to the location element.

As for the error regarding the antiforgery token, make sure that it is present on your view (by using @Html.AntiForgeryToken() ) and that no script removes it from the DOM before you submit the form

Community
  • 1
  • 1
Dennis Wanyonyi
  • 348
  • 1
  • 4
  • 17