Quick question;
Giving the controller User and the and function Get someone could call;
mydomain.com/user/get and access the functionality over there.
What are the best practices to allow only my application to be able to call that?
Thanks
Decorating the Action Method with the [ChildActionOnly] attribute will ensure that the Action Method on a controller cannot be directly invoked by a user entering a URL. EG
[ChildActionOnly]
public ActionResult _CantNavigateHere()
{
return View();
}
You can use the AntiForgeryToken with MVC3 and Ajax.
This is how I do it. (this isn't foolproof of course, but adds another layer of security)
On your view, anywhere, output:
@Html.AntiForgeryToken()
And in your Ajax call include this value in the post variables like:
$.post("/Controller/Action/",
{ 'id': var_id, '__RequestVerificationToken': $("input[name='__RequestVerificationToken']").val() },
function (data) { /* handle request */ });
And in your Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Action(int id) {..}
Besides this, you can also do a check for the referring host and reject any host that doesn't match your domain. This can be spoofed, but again, is just another layer to add.