0

I make my EDIT view a dialog box and i need to pass AntiForgeryToken with it. But I've tried several times and still I can't pass it.

$("#EditSave").click(function(e) {
    e.preventDefault();                           
    $.ajax({
        url: 'PIS/Edit',
            type: "POST",
            dataType: 'json',
            data: $('form').serialize(),                  
            success: function (data) {
                if (data.Save) {
                    dialog.dialog('close');
                    var notification = {
                        type: "success",
                        title: "Successfully",
                        message: "save ",
                        icon: 'glyphicon glyphicon-ok-sign'
                    };
                    showNotification(notification);
                    updatePartial();
                }
                if (data.AlreadyExist) {
                    var notification = {
                        type: "danger",
                        title: "Warning!",
                        message: "Already Exist",
                        icon: 'glyphicon glyphicon-exclamation-sign'
                    };
                    showNotification(notification);
                }
                if (data.Back) {
                    var notification = {
                        type: "danger",
                        title: "Warning!",
                        message: "Information was not successfully saved. Please check required fields.",
                        icon: 'glyphicon glyphicon-exclamation-sign'
                    };
                    showNotification(notification);
                }
                else {
                    debugger
                    dialog.dialog('close');
                }

            },
            error: function (xhr, status, error) {
                alert(xhr.responseText);
            }
    })
    return false;
})

This is my controller action and I need to pass the token on my view so that cross-side scripting can be prevented because this is an Edit view in a form of a dialog box.

 public async Task<PartialViewResult> Edit(string id)
    {         
        return PartialView();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<JsonResult>Edit(PISEditViewModel viewModel)
    { 
        return Json(new {Back = true });
    }

This is the error message i got when passing $('form').serialize()

The anti-forgery token could not be decrypted. If this application is hosted by a Web Farm or cluster, ensure that all machines are running the same version of ASP.NET Web Pages and that the <machineKey> configuration specifies explicit encryption and validation keys. AutoGenerate cannot be used in a cluster.

  • Can you please format your code snippet and also include the view (markup) and action method? Could you also specify what error you're getting/how you know it doesn't work? – Andrei Olariu Oct 07 '16 at 07:30
  • `$('form').serialize();` will include the token (assuming you have rendered it inside the form correctly –  Oct 07 '16 at 07:32
  • When i pass $('form').serialize(); i get the error message "The Anti-Forgerytoken could not be decrypted" – Jefferson Atilano Oct 07 '16 at 07:43
  • Add the **full** details of the error message in your question (not comments) –  Oct 07 '16 at 07:45
  • I think in your page _RequestVerificationToken is used twice, check it out. – LateshtClick.com Oct 07 '16 at 07:55
  • Check these related issues: http://stackoverflow.com/questions/23402210/the-anti-forgery-token-could-not-be-decrypted and http://stackoverflow.com/questions/15788912/deploying-antiforgerytoken-error. Usually this error comes from multiple AFTs inside a view. – Tetsuya Yamamoto Oct 07 '16 at 08:32
  • I just one AFTs in my form but my problem is how to pass AFTs using AJAX not on submit form,, – Jefferson Atilano Oct 10 '16 at 03:16

0 Answers0