0

I have jQuery code that post data with JSON.stringify to controller class but when I used AntiForgeryToken, it doesn't work.. is any better way to secure JSON post or I am missing out something....

secondly do i need additional to this .. i.e. encryption to secure JSON data...

many thanks for help in advanced...

<script type="text/javascript">

$(document).ready(function () {
    $('#id_login_submit').click(function () {

        var _authetication_Data = { _UserName: $('#u1').val(),  _Password: $('#p1').val() }

        $.ajax({
            type: "POST",
            url: "/Account/ProcessLoginRequest",
            data: JSON.stringify({ model: _authetication_Data }),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (response) {
                alert(response);
            }


        });

    });
});

 </script>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    @Html.LabelFor(m => m._UserName)
    @Html.TextBoxFor(m => m._UserName, new { id = "u1"})


    @Html.LabelFor(m => m._Password)
    @Html.PasswordFor(m => m._Password, new { id = "p1"})


    <input type="button" id="id_login_submit" value="Login" />
}

   [HttpPost]
    [ValidateAntiForgeryToken]
    public JsonResult ProcessLoginRequest(LoginModel model)
    {
        string returnString = null;

      if (ModelState.IsValid && WebSecurity.Login(model._UserName, model._Password, persistCookie: true))
        {
            returnString = "user is authenticated";
        }

        else
        { returnString = "Message from loginProcess"; }

        return Json(returnString, JsonRequestBehavior.AllowGet);
    }
tereško
  • 58,060
  • 25
  • 98
  • 150
K.Z
  • 5,201
  • 25
  • 104
  • 240

2 Answers2

2

The problem is that you are not including the VerificationToken with your request:

var _authetication_Data = { _UserName: $('#u1').val(),  _Password: $('#p1').val(), __RequestVerificationToken: $('[name=__RequestVerificationToken]').val(); }
Andy T
  • 10,223
  • 5
  • 53
  • 95
  • Thanks Queti, I am getting Json response but is it really authenticating token because when i modify token code on client side and is still working which my guessing it shouldn't?? is there any way i can confirm the authentication with correct token??? thanks you – K.Z Dec 16 '12 at 12:41
  • var token = $('input[name=__RequestVerificationToken]').val()+"999999"; – K.Z Dec 16 '12 at 12:48
1

this is how i use code

<script type="text/javascript">

$(document).ready(function (options) {
    $('#id_login_submit').click(function () {

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

      //var token = $('input[name=__RequestVerificationToken]').val()+"999999";
     //   alert("token :: "+token);

        var _authetication_Data = { _UserName: $('#u1').val(), _Password: $('#p1').val(), "__RequestVerificationToken": token }


            $.ajax({
                type: "POST",
                url: "/Account/ProcessLoginRequest",
                data: JSON.stringify({ model: _authetication_Data }),
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (response) {
                    alert(response);
                }
            });

    });
});

K.Z
  • 5,201
  • 25
  • 104
  • 240