3

I just ported a web application from ASP.NET Core 2.1 to 3.0 and I'm getting that form key length error:

InvalidDataException: Form key length limit 2048 or value length limit 2147483647 exceeded. Microsoft.AspNetCore.WebUtilities.FormPipeReader.ThrowKeyOrValueTooLargeException()

In my startup.cs I've got the following snippet:

services.AddMvc()
    .SetCompatibilityVersion( CompatibilityVersion.Version_3_0 )
    .AddSessionStateTempDataProvider()
      // Maintain property names during serialization. See:
      // https://github.com/aspnet/Announcements/issues/194
      .AddNewtonsoftJson( options =>
      {
        options.SerializerSettings.ContractResolver = new DefaultContractResolver();
        options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
      } );


  // maxout form sizes
  services.Configure<FormOptions>( options =>
  {
    options.ValueCountLimit = int.MaxValue;
    options.ValueLengthLimit = int.MaxValue;
    options.MultipartHeadersLengthLimit = int.MaxValue;
  } );

But, as in the past, this doesn't seem to be working again. Maybe I've got the wrong order for where the form options need to be placed?

I'd rather not decorate the actions of 100 controls, if possible; I'd rather set this globally.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
infocyde
  • 4,140
  • 2
  • 25
  • 36
  • can you pinpoint the line of code before this gets thrown? – jazb Oct 04 '19 at 05:51
  • Possible duplicate of [Form key or value length limit 2048 exceeded](https://stackoverflow.com/questions/43305220/form-key-or-value-length-limit-2048-exceeded) – jazb Oct 04 '19 at 05:52
  • Did you mean that error still occured after you configure FormOptions in the Startup.cs ? I fail to reproduce the issue , could you share a demo that can reproduce your issue ? About the default setting of form size , you could refer to the [source code](https://github.com/aspnet/HttpAbstractions/blob/8e4f7365adf304e9704b1db43e3095a267583513/src/Microsoft.AspNetCore.WebUtilities/FormReader.cs#L18) of FormReader class. – Xueli Chen Oct 07 '19 at 09:52

1 Answers1

4

Try adding an additional option for KeyLengthLimit, that fixed it for me.

services.Configure<FormOptions>(options =>
{
    options.KeyLengthLimit = int.MaxValue;
    options.ValueCountLimit = int.MaxValue;
    options.ValueLengthLimit = int.MaxValue;
    options.MultipartHeadersLengthLimit = int.MaxValue;
});
Granicus
  • 854
  • 10
  • 19