我想将 Web 服务与 Ajax 一起使用。使用 ajax 调用 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>