0

In SendFile.cshtml.cs

[RequestFormLimits(MultipartBodyLengthLimit = 2097152)]
public class SendFileModel : PageModel

In SendFile.cshtml

<form method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label asp-for="Data.UploadFile"></label>
        <input asp-for="Data.UploadFile" class="form-control" accept=".zip" />
    </div>
    <div class="form-group">
        <label asp-for="Data.EmailAddress"></label>
        <input asp-for="Data.EmailAddress" class="form-control" placeholder="name@example.com" />
    </div>
    <input type="submit" value="Send" class="btn btn-primary" />
</form>

It is causing the following exception when the file is larger than the limit:

An exception was thrown while deserializing the token.

Exception: 
Microsoft.AspNetCore.Antiforgery.AntiforgeryValidationException: The antiforgery token could not be decrypted.
 ---> System.Security.Cryptography.CryptographicException: The key {xxxxxxxxxx} was not found in the key ring.
   at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.UnprotectCore(Byte[] protectedData, Boolean allowOperationsOnRevokedKeys, UnprotectStatus& status)
   at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.DangerousUnprotect(Byte[] protectedData, Boolean ignoreRevocationErrors, Boolean& requiresMigration, Boolean& wasRevoked)
   at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.Unprotect(Byte[] protectedData)
   at Microsoft.AspNetCore.Antiforgery.DefaultAntiforgeryTokenSerializer.Deserialize(String serializedToken)
   --- End of inner exception stack trace ---
   at Microsoft.AspNetCore.Antiforgery.DefaultAntiforgeryTokenSerializer.Deserialize(String serializedToken)
   at Microsoft.AspNetCore.Antiforgery.DefaultAntiforgery.GetCookieTokenDoesNotThrow(HttpContext httpContext)

Is this correct, or is there a better way to set a upload limit and handle when the file is too large?

Thanks

  • Does this answer your question? [Multipart body length limit exceeded exception](https://stackoverflow.com/questions/40364226/multipart-body-length-limit-exceeded-exception) – Roar S. Jan 12 '21 at 13:43
  • Your error is not caused by the code you gave. You could use [DataProtection](https://stackoverflow.com/questions/42542923/the-antiforgery-token-could-not-be-decrypted). It could not be configured correctly. – Karney. Jan 13 '21 at 07:06
  • The error only happens if the file selected is larger than the max file size I have set. Rest of the time the token is fine – user5255727 Jan 13 '21 at 15:10

0 Answers0