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>

--

--

--

Full Stack Developer

Recommended from Medium

GIT IT DONE

Expert Session on Industry Use Case of Kubernetes and RedHat OpenShift

Started with Vagrant

Business Intelligence in Software Projects

Cross-Platform Vs Native Mobile Application Development

CSI World Infotech, cross-platform mobile application development vs native mobile application development

What is PaaS?

First Principles Python Part 1

10 Best Scala Tutorials for Beginners [2022 May]— Learn Scala Online

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
ilhan DEMİRTEPE

ilhan DEMİRTEPE

Full Stack Developer

More from Medium

Black-box 2 — INE Penetration Testing Basics

HOW TO MANIPULATE COOKIES: AN INFORGRAPHIC FOR HACKERS

Securing Lichess one move at a time

C.O.R.S. (Cross Site Resource Sharing)