This is a migrated thread and some comments may be shown as answers.

[Solved] ASP.NET MVC: Passing antiforgery token

2 Answers 18 Views
Chart
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Michael
Top achievements
Rank 1
Michael asked on 03 Feb 2012, 11:55 PM
Hey, I was wondering how to pass an antiforgery token whenever making an AJAX request to retrieve chart data.

For example:

@(Html.AntiForgeryToken())
...
@( Html.Telerik().Chart(Model)
       .Name("chart")
       .Title("A Fancy Chart")
       .Series(series =>
       {
           series.Line("Value");
       })
       .DataBinding(dataBinding => dataBinding.Ajax().Select("_LineChartData""Home"new { datapoints = 15 }))
       .CategoryAxis(axis => axis
            .Categories(s => s.Id))
       .ValueAxis(axis => axis
           .Numeric())
       .HtmlAttributes(new { style = "width: 400px; float:left;" })
   )

I tried the method outlined here "http://www.telerik.com/community/forums/aspnet-mvc/grid/antiforgerytoken-on-insert.aspx" but ClientEvents does not have the OnSave method.
So I tried using the OnLoad method as shown here "http://www.aspnetwiki.com/page:implementing-the-anti-forgery-token-with-the-telerik-mv" but found out (through log strings) that the ajaxSend method never fires.
I also tried to modify the ajax request via jquery.ajax.prefilter(), but that did not work either.

Can anyone suggest a solution? Right now the chart data won't load unless I explicitly disable the CSRF protection set up in the controller.

2 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 08 Feb 2012, 02:56 PM
Hi Michael,

The token can be send by using jQuery ajaxPrefilter or ajaxSetup methods. However, you should use them earlier than the OnLoad event as it is triggered after the request has been made.

Kind regards,
Daniel
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Michael
Top achievements
Rank 1
answered on 08 Feb 2012, 06:59 PM
Thanks for the reply. The ajaxSetup method worked, while prefilter did not for some reason.

Here is the code (for anyone interested):

<form id="__AjaxAntiForgeryForm" action="#" method="post">@Html.AntiForgeryToken()</form>

<script type="text/javascript">
 
    $.ajaxSetup({
        data: { '__RequestVerificationToken': $('#__AjaxAntiForgeryForm input[name=__RequestVerificationToken]').val() }
    });
 
</script>
Tags
Chart
Asked by
Michael
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Michael
Top achievements
Rank 1
Share this question
or