1

Whenever an error is thrown in the try-catch block I want to return to the view and display to the user the error. This works fine when I run it on my local machine. However when I push onto the production server I get the error

The required anti-forgery cookie "__RequestVerificationToken" is not present

Controller:

    [HttpPost]
    [ValidateAntiForgeryToken]
    [Authorize(Roles = @"Admin")]
    public ActionResult Post(CpjsProcessFormViewModel viewModel)
    {
        if (ModelState.IsValid)
        {
            try
            {
                if (!string.IsNullOrEmpty(viewModel.UserName) && !string.IsNullOrEmpty(viewModel.Password))
                {
                    _sysproCredentials.UserName = viewModel.UserName;
                    _sysproCredentials.Password = viewModel.Password;
                }

                var serviceController = new ServiceController(_unitOfWork,
                    new ControlPlanJobProcessor(_sysproService, _sysproCredentials));
                serviceController.ProcessControlPlan(viewModel.ControlPlanId, viewModel.CloseJob, true,
                    User.Identity.GetUserId());
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", e.Message);
                return View(viewModel);
            }

            return RedirectToAction("Post", new { id = viewModel.ControlPlanId, message = "Successfully posted to Syspro" });
        }

        return View(viewModel);
    }

View:

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.HiddenFor(model => model.ControlPlanId)
<div class="form-horizontal">
    @Html.ValidationSummary(false, "", new { @class = "text-danger" })
}
fore_right
  • 69
  • 1
  • 8
  • Possible duplicate of https://stackoverflow.com/questions/33306859/the-required-anti-forgery-cookie-requestverificationtoken-is-not-present – Jamin Mar 31 '18 at 05:30
  • None of those suggestions in that thread helped me. In chrome there are no response cookies in both dev or prod but in dev I do not get an error. I've also tried setting an explicit – fore_right Mar 31 '18 at 15:16
  • 1
    I think your markup is goofing up your form submission. The `
    ` that is opened inside the `using Html.BeginForm()` but not closed prior to the closing of your using statement could be causing POST issues. The VS editor typically complains about such occurrences.
    – Tommy Mar 31 '18 at 23:26

0 Answers0