views:

240

answers:

2

I have a basic JsonResult method that is being called by a jQuery $.ajax call in my view.

    [AcceptVerbs(HttpVerbs.Post)]
    public JsonResult DoWork(string param1)
    {
        // do something important 

        return Json();
    }

So my question is, could this method be called/hacked and passed erroneous data? Let's say it was to create a new user int the system. Could I fake out a call to this method? Should I some how be protecting this method using some kind of Anti-forgery token or anything?

+1  A: 

Yes. This method can be called just like any other public controller action.

gautema
+3  A: 

Yes, you should protect it. Anyone can call this method, and pass any value they want. You should always distrust the data you receive.

You could ofcourse secure it using the Authorize-attribute:

[Authorize(Roles='...')]

or use any other method to identify and authorize the user.

Edit:

I found an article with some more information about using the AntiForgeryToken in Ajax: http://www.sogeti-phoenix.com/Blogs/post/2009/05/MVC-ndash3b-Using-AntiForgeryToken-over-AJAX.aspx

I haven't tested this though.

Pbirkoff
right but lets say i didn't have roles and such defined, is there anyway i could do use an anti forgery token?
aherrick
without roles, how do you know who can add users and who not?
Pbirkoff
i'm just giving an example of adding users. there is no way i can protect it so that i know it is coming from the view as a legit request?
aherrick
here's an article that might help you: http://www.sogeti-phoenix.com/Blogs/post/2009/05/MVC-ndash3b-Using-AntiForgeryToken-over-AJAX.aspx
Pbirkoff