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

Pass Antiforgery token with Server Bound Grid

4 Answers 116 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 17 May 2013, 07:07 PM
I'm using asp.net mvc 4 with a server bound grid.  When the actions are posted to my controller, they do not include the antiforgery token and therefore .net throws up an error.  I've seen examples on how to pass the token with ajax bound grids, but not with server bound grids.  How would this be accomplished?

4 Answers, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 21 May 2013, 03:44 PM
Hello Curt,

Unfortunately the Grid does not does not offer you a way to append additional parameters to the URLs that are generated for the different links - such as paging / sorting etc.

The only work-around I can suggest you is to override the whole transport option like this before initializing the Grid:

<script type="text/javascript">
    $.extend(true, kendo.data, {
        transports: {
            "aspnetmvc-server": kendo.data.RemoteTransport.extend({
                read: function (options) {
                    var url,
                        prefix = this.options.prefix,
                        params = [prefix + "sort",
                            prefix + "page",
                            prefix + "pageSize",
                            prefix + "group",
                            prefix + "aggregate",
                            prefix + "filter"],
                        regExp = new RegExp("(" + params.join('|') + ")=[^&]*&?", "g"),
                        query;
 
                    query = location.search.replace(regExp, "").replace("?", "");
 
                    if (query.length && !(/&$/.test(query))) {
                        query += "&";
                    }
 
                    options = this.setup(options, "read");
 
                    url = options.url;
 
                    if (url.indexOf("?") >= 0) {
                        url += "&" + query;
                    } else {
                        url += "?" + query;
                    }
 
                    url += $.map(options.data, function (value, key) {
                        return key + "=" + value;
                    }).join("&");
                    
                   //here you can try to modify the URL and REPLACE (if existing) the antiforgery token
 
                    location.href = url;
                }
            })
        }
    });
</script>
 

I am sorry that the server binding does not support a way to send additional parameters.

Kind Regards,
Petur Subev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Shane
Top achievements
Rank 1
answered on 15 Oct 2015, 09:15 PM
Is this still the only solution for sending the antiforgery token when using a server binding grid?  I'm just asking as it's over 2-year-old post.
0
Alexander Popov
Telerik team
answered on 20 Oct 2015, 08:09 AM
Hello Shane,

You can try passing the token through the route values object, for example: 
.Read("ServerEditing", "Grid", new { @__RequestVerificationToken =  token})

Regards,
Alexander Popov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Brad
Top achievements
Rank 1
answered on 26 May 2016, 05:37 PM

Since this is such a common operation for both server and Ajax bound grids (post method), would it not be valuable to add a method for this?  Maybe something like this:

.Read("ServerEditing", "Grid", new { @__RequestVerificationToken =  token}).AppendRequestVerificationToken()

Just a thought.
Tags
Grid
Asked by
Chris
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Shane
Top achievements
Rank 1
Alexander Popov
Telerik team
Brad
Top achievements
Rank 1
Share this question
or