Asp.Net MVC4 stopping people requesting or posting data to my API

丶灬走出姿态 提交于 2020-01-25 07:36:06

问题


I want to be able to use AJAX to Get/Post to an API controller in my MVC4 application.

However, I don't want anyone to be able to setup a web page and get/post to the controller - only from web pages delivered from my server.

So, should I just use a normal controller and return data, rather than an API controller? (or have I misunderstood that using the API controller opens up the web application to any Get/Post?)

Thanks, Mark


回答1:


This is a possible duplicate of my question here. In a nutshell, the only way to secure API endpoints is to have the clients authenticate somehow. This can be done with a normal ASP.NET auth cookie, if you decorate the controllers / actions with the [Authorize] attribute. You could also roll your own solution using API keys or whitelist IP addresses. See this link for a laundry list of options.

The answer I posted to my own question does make it more difficult for non-consumers of my web app to invoke API endpoints, though as Darin points out, it can be circumvented. Users could go to the web app to obtain the encrypted AnonymousID and custom encryption cookie, plug those into Fiddler2 (or any other client), and hit the API as much as they wanted for 24-48 hours.



来源:https://stackoverflow.com/questions/15877034/asp-net-mvc4-stopping-people-requesting-or-posting-data-to-my-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!