Share via


The required anti-forgery form field "__RequestVerificationToken" is not present.

Question

Monday, November 2, 2020 1:50 PM

Hi,

I have this javascript function  which is to send the file to action result.

function fileChanged(e) { // Upload file - User select file/s
        debugger;
        var fileInput = $(e)[0];
        var data = new FormData();
        var token = $("input[name='__RequestVerificationToken']").val();
        data.append('__RequestVerificationToken', token);
        data.append('files', fileInput.files[0]);
        $.ajax({
            type: "POST",
            url: '@Url.Action("Upload", "Dashboard")',
            contentType: false,
            processData: false,
            data: data,
            success: function (result) {
                if (result.result) {

                }
                else {

                }
            },
            error: function () {

            }
        });
    }

And the Action in controller as below:

 [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Upload(HttpPostedFileBase files)
        {
              // My Code
         }
            

Also in the View I added

}
@using (Html.BeginForm("Apply", "Dashboard", FormMethod.Post,
                new
                {
                    autocomplete = "off",
                    enctype = "multipart/form-data",
                }))
{
    @Html.AntiForgeryToken()

but still I'm facing the error: The required anti-forgery form field "__RequestVerificationToken" is not present.

Please Help

All replies (8)

Tuesday, November 17, 2020 8:17 AM âś…Answered

Hi Khalid Salameh,

Khalid Salameh

 but still the same, but If I will using" window.location.href" it is working.    I think the issue with ajax call, without ajax it is working, with ajax is not.</div>

Do you make sure your ajax is like this:

function fileChanged(e) { // Upload file - User select file/s
        debugger;
        $.ajax({
            type: "POST",
            url: '@Url.Action("Upload", "Dashboard")',
            contentType:'application/x-www-form-urlencoded; charset=UTF-8',
            data: { __RequestVerificationToken: $("input[name='__RequestVerificationToken']").val() },
            success: function (result) {
                if (result.result) {

                }
                else {

                }
            },
            error: function () {

            }
        });
    }

You could press F12 when you are running the project and break point at the ajax to check the error.

https://stackoverflow.com/questions/45773645/the-required-anti-forgery-form-field-requestverificationtoken-is-not-present

Best regards,

Yijing Sun


Monday, November 2, 2020 3:55 PM

I use this to get the token is JS, which is slightly different than what you are using:

       ajax: {
            url: RootUrl + "xxx/JsonData/" + ParentId,
            type: "POST",
            data: { __RequestVerificationToken: $("[name='__RequestVerificationToken']").val() }
        }

Not sure what FormData() is, but that might be causing some of your problem.


Monday, November 2, 2020 6:43 PM

Hi ryanbesko,

thanks for your reply, but unfortunately I tried as you mentioned, but still I got same error.


Tuesday, November 3, 2020 7:59 AM

Hi Khalid Salameh,

As far as I think,you could clearing site data for the site:

Best regards,

Yijing Sun


Tuesday, November 3, 2020 8:06 AM

Hi yij,

I tried now to clear storage, I got same error: "System.Web.Mvc.HttpAntiForgeryException: 'The required anti-forgery form field "__RequestVerificationToken" is not present."


Friday, November 6, 2020 7:19 AM

Hi Khalid Salameh,

You could check this in the web.config:

<httpCookies requireSSL="true" />

More details,you could refer to below article:

https://stackoverflow.com/questions/16102957/the-required-anti-forgery-form-field-requestverificationtoken-is-not-present

Best regards,

Yijing Sun


Thursday, November 12, 2020 12:37 PM

Hi Yij,

I tried <httpCookies requireSSL="true"/>   but still the same, but If I will using" window.location.href" it is working.    I think the issue with ajax call, without ajax it is working, with ajax is not.


Wednesday, December 2, 2020 2:09 PM

Thanks for your reply ..