我使用ASP.NET的通用处理程序(ASHX文件)来制作可以将数据返回到移动应用程序等的服务器端页面。其中一些数据是私有的。我正在使用JSON&截至目前的POST方法。
然而,任何有权访问客户端代码的人(例如移动应用程序的代码)都能够看到他必须将哪些关键字发送到“不可预测的”URL才能进行更改或获取数据
我已经进行了几个小时的研究,无法找到一种正确的方法来验证发送JSON请求的人确实是一个获得批准的移动应用程序。
使用AJAX向服务器发送请求的示例:
function login()
{
var jsonParam = { name: "test", aname: "secret", pass: "1234", type: "login" }
$.ajax({
url: "AppDatabase.ashx",
type: "post",
data: JSON.stringify(jsonParam),
dataType: "json",
contentType: 'application/json; charset=utf-8',
async:false,
success: function (response) {
alert(response.userEmail);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Status: " + textStatus + "\r\n" + "Error: " + errorThrown);
}
});
}
在服务器端接收请求的示例:
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "application/json";
JavaScriptSerializer jss = new JavaScriptSerializer();
context.Request.InputStream.Position = 0;
string json;
using (var reader = new StreamReader(context.Request.InputStream))
{
json = reader.ReadToEnd();
}
Dictionary<string, object> dict = jss.Deserialize<Dictionary<string, object>>(json);
if(dict["aname"].ToString() == "secret")
{
// The best security I have right now is a "secret keyword" within the request
}
}
答案 0 :(得分:0)
你目前的方式非常危险。
您可以使用另一个需要用户名和页面的网页要验证的密码(例如,使用Session
),然后让客户端请求您的 ashx 文件, ashx 应检查Session
进行身份验证。
答案 1 :(得分:0)
只是一个想法:
客户端(例如移动应用)是“你的”还是这是某种API(适用于x客户端)?
无论哪种方式,也许值得研究JWT?我们的想法是“有效载荷”(数据)签名(例如HMAC SHA-256),并对“重播”提供保护。
这样,你不仅仅是在寻找auth部分 - re:两端都需要验证数据,这样才能确保这些数据来自“正确的来源”并且仍然是)有效。
... H个