我正在使用ASP.NET的通用處理程序(ASHX文件)製作可將數據返回到移動應用程序等的服務器端頁面。這些數據中的一部分是私密的。截至目前,我正在使用JSON & POST方法。如何使用ASHX處理程序進行正確的身份驗證
但是,任何人都可以訪問一個客戶端代碼(例如移動應用的代碼),將能夠看到哪些關鍵字有發送到「不可預知」的網址,以便進行更改或得到數據。
我已經做了好幾個小時的研究,但沒有找到驗證了一個發送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
}
}
你的意思是你從客戶端發送祕密關鍵字? –
@Furqan是的,還有呢? – Kfirprods
如果你想停止任何其他第三方應用程序訪問URL,那麼這是不可能的。一旦URL被公開,任何人都可以訪問,唯一的辦法就是通過實現用戶認證。但是,如果第三方擁有憑據,則仍然可以訪問資源。 – sallushan