我希望使用Ajax调用Web服务。在使用Ajax调用Web服务时,最好的安全方式是什么?我想防止应用程序外部的远程调用。
例如,我有以下Web服务:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class Users : System.Web.Services.WebService
{
[WebMethod]
public List<User> GetUsers()
{
List<User> listUsers = new List<User>();
User user = new User();
user.Id = 1;
user.Name = "John";
User user2 = new User();
user2.Id = 2;
user2.Name = "Martin";
listUsers.Add(user);
listUsers.Add(user2);
return listUsers;
}
}
}
我使用jQuery Ajax调用Web服务:
<script type="text/javascript">
$(function () {
getUsers();
function getUsers() {
$.ajax({
type: "POST",
url: "Webservices/Users.asmx/GetUsers",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var users = response.d;
$.each(users, function (index, user) {
console.log(user.Name);
});
},
failure: function (msg) {
}
});
}
});
</script>