ASP.NET Core 2.2 AntiForgeryToken with JSON.Stringify

ASP.NET Core 2.2 AntiForgeryToken with JSON.Stringify

I have this controller on my server that has a ValidateAntiForgeryToken attribute

I have this controller on my server that has a ValidateAntiForgeryToken attribute

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult GetHistory([FromBody] ChatMessageGetHistoryViewModel Input)
{
     var userName = HttpContext.User.Claims.Where(c => c.Type == "UserName").Select(c => c.Value).SingleOrDefault();
     var history = chatMessageData.GetAllBySessionId(Input.SessionId, userName);
     var output = JsonConvert.SerializeObject(history);
     return Ok(output);
}

I have tried this method but so far I keep getting the error code 400. I have tried including the antiforgery token as part of the form data but that also doesn't work.

<script>
    $(document).ready(function () {
        var token = $('input[name="__RequestVerificationToken"]', $('#__AjaxAntiForgeryForm')).val();
        var SessionId = document.getElementById("Id").value;
        var form_data = {
            "SessionId": SessionId,
            __RequestVerificationToken: token,
        };
        $.ajax({
            url: "@Url.Action("GetHistory", @ViewContext.RouteData.Values["controller"].ToString())",
            method: "POST",
            data: JSON.stringify(form_data),
            contentType: "application/json",
            success: function (result) {
                console.log(result);
                var output = JSON.parse(result);
                for (var i = 0; i < output.length; i++) {
                    var p = document.createElement("span");
                    var q = document.createElement("li");
                    if (output[i].Mine == true) {
                        p.setAttribute("class", "Sender Me");
                        q.setAttribute("class", "Message");
                    } else {
                        p.setAttribute("class", "Sender");
                        q.setAttribute("class", "Message");
                    }
                    p.textContent = output[i].Name + " - " + moment(output[i].CreatedOn).format("DD-MM-YYYY HH:mm:ss");
                    q.textContent = output[i].Message;
                    document.getElementById("MessageList").appendChild(p);
                    document.getElementById("MessageList").appendChild(q);
                }

        },
        error: function (error) {
            console.log(error);
        }


Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Jquery from validation || ASP NET Core client side validation

LIKE | COMMENT | SHARE | SUBSCRIBE In this video, I will show you how to add form validation using jquery. Subscribe & Stay Connected! Thank You! ♥ #Identity...

ASP.NET MVC Application with JQuery, AJAX

Let's have a sample example: We have a submit button in our JQuery AJAX MVC application. Let's try to show a message when it is clicked. ASP.NET MVC Application - Using JQuery, AJAX

Add asp.net Identity in empty project

LIKE | COMMENT | SHARE | SUBSCRIBE Today, in this tutorial you will see the process of adding an ASP.NET Identity to the ASP.NET Web Application developed in...