How to make ajax request with anti-forgery token in mvc

前端 未结 5 1622
时光取名叫无心
时光取名叫无心 2020-12-05 00:47

I have problem with below details from MVC project.

When I am trying to use jquery ajax request with loading panel like spinning gif (or even text), I am getting err

相关标签:
5条回答
  • 2020-12-05 00:48

    Have you added the token to your View? Like this:

    <form method="post" action="/my-controller/my-action">
        @Html.AntiForgeryToken()
    </form>
    

    Since your controller receiving the post is looking for the anti forgery token, you need to ensure that you add it to your form in the view.

    EDIT:

    Try building your data in json first:

    var formData = $('form[action="/TransportJobAddress/Create"]').serialize();
    $.extend(formData, {'__RequestVerificationToken': token });
    
    //and then in your ajax call:
    $.ajax({
        //...
        data:formData
        //...
    });
    
    0 讨论(0)
  • 2020-12-05 01:02

    I wanted to secure both Ajax and normal request, so here is what I came out with:

    First using the excellent blog from haacked.com I Created the ConditionalFilterProvider as described.

    Then I created all the classes as described on the blog from codethinked.

    On my _layout page I added the piece with the $.ajaxPrefilter as described in the blog... This assures that all my Ajax-callback now send the Antiforgery token through header.

    To glue all together I added this piece of code on my global.asax / Application_Start

    (c, a) =>
                        (c.HttpContext.Request.IsAjaxRequest() &&
                         !string.Equals(c.HttpContext.Request.HttpMethod, "GET"))
                            ? new AjaxValidateAntiForgeryTokenAttribute()
                            : null,
    (c, a) =>
                        (!c.HttpContext.Request.IsAjaxRequest() &&
                         string.Equals(c.HttpContext.Request.HttpMethod, "POST", StringComparison.OrdinalIgnoreCase))
                            ? new ValidateAntiForgeryTokenAttribute()
                            : null
    

    Basically.. inject the attribute to all my Controllers that are not GET.

    After that I just had to go to all my (Very few) forms and add the @Html.AntiForgeryToken().

    To proof that all worked I just try to dens things with a form without the AntiForgeryToken and get the expected exception. And remove the $.ajaxPrefilter and create Ajax requests and the expected exception was received.

    0 讨论(0)
  • 2020-12-05 01:03

    Rather than manually adding it to each request, I usually do something like this:

    var token = $('input[name="__RequestVerificationToken"]').val();
    $.ajaxPrefilter(function (options, originalOptions) {
      if (options.type.toUpperCase() == "POST") {
        options.data = $.param($.extend(originalOptions.data, { __RequestVerificationToken: token }));
      }
    });
    

    This will automatically add your token to any ajax POST you do.

    0 讨论(0)
  • 2020-12-05 01:09

    Have you added your token to the header of the ajax call?

    You need to add AntiForgeryToken in your message header in the ajax call:

    var token = $('input[name="__RequestVerificationToken"]').val();
    
    var headers = {};
    
    headers['__RequestVerificationToken'] = token;
    
    $.ajax({
            url: ... some url,
            headers: headers,
            ....
    });
    

    Try this in your code:

    var token = $('input[name="__RequestVerificationToken"]').val();
    var tokenadr = $('form[action="/TransportJobAddress/Create"] input[name="__RequestVerificationToken"]').val(); 
    
    var headers = {};
    var headersadr = {};
    headers['__RequestVerificationToken'] = token;
    headersadr['__RequestVerificationToken'] = tokenadr;
    
    $('#submitaddress').click(function subaddr(event) {
        event.preventDefault();
        event.stopPropagation();
      //$('#addAddress').html('<img src="/img/animated-overlay.gif"> Sending...');
       // $('#addAddress').blur();
        //  $(this).bl
        if ($('#Jobid').val()!="") {
            $('#TransportJobId').val(parseInt($('#Jobid').val()));
            $.ajax(
                  {
                      url: '/TransportJobAddress/create',
                      type: 'POST',
                      headers:headersadr, 
                      data: "__RequestVerificationToken=" + token + "" + $('form[action="/TransportJobAddress/Create"]').serialize(),
                      success: function poste(data, textStatus, jqXHR) { $('#addAddress').html(data); return false; },
                      error: function err(jqXHR, textStatus, errorThrown) { alert('error at address :' + errorThrown); }
                  });
        }
        else {
            var transportid = 2;
            $.ajax({
                url: '/TransportJob/create',
                type: 'POST',
                headers:headers, 
                data: $('form[action="/TransportJob/Create"]').serialize(),
                success: function sfn(data, textStatus, jqXHR) {
                    transportid = parseInt(data);
                    $('#Jobid').val(data);
                   // alert('inserted id :' + data);
                    $('#TransportJobId').val((transportid));
                    $.ajax(
             {
    
                 url: '/TransportJobAddress/create',
                 type: 'POST',
                 //beforeSend: function myintserver(xhr){  
                 //        $('#addAddress').html('<div id="temp_load" style="text-align:center">please wait ...</div>');
                 //},
                 headers:headers, 
                 data: $('form[action="/TransportJobAddress/Create"]').serialize(),
                 success: function poste(data, textStatus, jqXHR) {
                     $('#addAddress').html(data);
    
                 },
                 error: function err(jqXHR, textStatus, errorThrown) {
                     alert('error at address :' + errorThrown);
                 }
    
             });
                },
                error: function myfunction(jqXHR, textStatus, errorThrown) {
                    alert("error at transport :" + jqXHR.textStatus);
                },
                complete: function completefunc() {
                  //  alert('ajax completed all requests');
                    return false;
                }
    
            });
        }
    });
    

    Added headers line in your ajax call.

    0 讨论(0)
  • 2020-12-05 01:10

    View or Layout:

    < form id='_id' method='POST'> @html.antiforgeryToken(); < /form>

    The ajax call function:

    var data={...};
    var token=$('#_id').serializeObject();
    var dataWithAntiforgeryToken = $.extend(data,token);
    
    $.ajax(
    {
    ....,
    data: dataWithAntiforgeryToken;
    }
    )
    
    0 讨论(0)
提交回复
热议问题