Note that there are some explanatory texts on larger screens.

plurals
  1. POCSRF Protection in AJAX Requests using MVC2
    primarykey
    data
    text
    <p>The page I'm building depends heavily on AJAX. Basically, there is just one "page" and every data transfer is handled via AJAX. Since overoptimistic caching on the browser side leads to strange problems (data not reloaded), I have to perform all requests (also reads) using POST - that forces a reload.</p> <p>Now I want to prevent the page against CSRF. With form submission, using <code>Html.AntiForgeryToken()</code> works neatly, but in AJAX-request, I guess I will have to append the token manually? Is there anything out-of-the box available?</p> <p><em>My current attempt looks like this</em>:</p> <p>I'd love to reuse the existing magic. However, <code>HtmlHelper.GetAntiForgeryTokenAndSetCookie</code> is private and I don't want to hack around in MVC. The other option is to write an extension like</p> <pre><code>public static string PlainAntiForgeryToken(this HtmlHelper helper) { // extract the actual field value from the hidden input return helper.AntiForgeryToken().DoSomeHackyStringActions(); } </code></pre> <p>which is somewhat hacky and leaves the bigger problem unsolved: How to verify that token? The default verification implementation is internal and hard-coded against using form fields. I tried to write a slightly modified <code>ValidateAntiForgeryTokenAttribute</code>, but it uses an <code>AntiForgeryDataSerializer</code> which is private and I really didn't want to copy that, too.</p> <p>At this point it seems to be easier to come up with a homegrown solution, but that is really duplicate code.</p> <p>Any suggestions how to do this the smart way? Am I missing something completely obvious?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload