Mvc Ajax Security


All The Code .
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult IndexPost(string myValue)
{
return Json(new { sendValue = myValue });
}
}
@{
ViewBag.Title = “Home Page”;
}
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = “myForm” }))
{
@Html.AntiForgeryToken()
}
<button type=”button” class=”btn-default” value=”Click Me” onclick=”GetToken()”></button>
<script src=”~/Scripts/jquery-3.3.1.js”></script>
<script type=”text/javascript”>
function GetToken() {
alert(12);
var form = $(‘#myForm’);
var token = $(‘input[name=”__RequestVerificationToken”]’, form).val();
$.ajax({
url:’/Home/IndexPost/’,
type: ‘POST’,
data: {
__RequestVerificationToken: token,
myValue: ‘My data’
},
success: function (result) {
console.log(token);
alert(result.sendValue);
}
});
return false;
};
</script>