Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I did a fortify code analysis in an existing MVC application and fortify recommend me to implement anti-forgery token in all HTTP POST calls as below.

JavaScript
var token = $('input[name="__RequestVerificationToken"]').val();

var headers = {};

headers['__RequestVerificationToken'] = token;

$.ajax({
        url: ... some url,
        headers: headers,
        ....
});


Adding __RequestVerificationToken in all Ajax calls across the application can be challenging.

Please help me to understand ways to implement anti-forgery token without modifying all ajax calls in an existing application?
Posted
Updated 3-Oct-18 8:52am
v2

Would you read this article, it seems to be pretty clear: http://blogs.perficient.com/microsoft/2014/02/asp-net-mvc-anti-forgery-token-demystified-part-1-what-is-it[^]?
(See other parts referenced from this page.)

Sorry if you already know all that; if so, please explain your concern more clearly.

—SA
 
Share this answer
 
This is what you want if you use JQUERY1-2. I'm looking to code this in pure JavaScript please.

var sToken = document.getElementsByName("__RequestVerificationToken")[0].value;
$.ajax({
url: "/Home/ClickCreateAccount/",
type: "POST",
contentType: "application/x-www-form-urlencoded",
data: { '__RequestVerificationToken': sToken, 'sMVCParameter1': sMVCParameter1, 'sMVCParameter2': sMVCParameter2 }
})
.done(function (data) {
//Process MVC Data here
})
.fail(function (jqXHR, textStatus, errorThrown) {
//Process Failure here
});
 
Share this answer
 
Comments
Richard Deeming 3-Oct-18 15:40pm    
This is not a solution to the question. Why have you posted it in the "Add your SOLUTION here" box?!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900