2012-04-03 62 views
0

我試圖做一個登錄頁面使用html,ajax & ASP.NET.The數據是真正傳遞給ajax函數,但是當我調試asp頁面的用戶名和密碼與NULL一起發送。 的代碼應該採取用戶名密碼&然後返回用戶標識登錄頁面使用ASP.Net和Ajax

Html頁面:

<div id="usernameid">Username:</div><input id="username" type="text"/> <span id="username_status"></span> 
    <div id="passwordid">Password:</div><input id="password" type="password"/> <span id="password_status"></span> 
    <div> <input id="loginbutton" onclick="UserLogin()" type="submit" value="Submit" /></div> 

的Javascript:

function UserLogin() { 
var postData = JSON.stringify({ "username": JSON.stringify($("#username").val()), "password": JSON.stringify($("#password").val()) }); 
alert(postData); 
$.ajax({ 
    type: "GET", 

    url: "http://localhost:49317/LoginPageForLearn.aspx", 
    data: postData, 
    contentType: "application/json; charset=utf-8", 
    dataType: "jsonp", 
    jsonp: 'jsoncallback', 
    success: callbackfunction, 
    error: function (msg) { alert(msg); } 
    }); 
    } 
    function callbackfunction(datacoming) { 
    localStorage["UserID"] = datacoming; 
    alert(datacoming); 

Asp.net頁面:

protected void Page_Load(object sender, EventArgs e) 
    { 

     string userName = ""; 
     int userId = -1; 
     string PassWord = ""; 
     if (Request.QueryString.Count != 0 && Request.QueryString["username"] != string.Empty && Request.QueryString["password"] != string.Empty) 
      { 
       userName = Request.QueryString["username"]; 
       PassWord = Request.QueryString["password"]; 
       userId = GetUserID(userName, PassWord); 

      } 
    } 

待辦事項你有什麼想法爲什麼不正確的數據傳遞?或者你有什麼其他的想法,我怎樣才能使用HTML做一個登錄頁面,並在SQL訪問數據。

非常感謝。

+3

您是通過QueryString以明文形式發送密碼的嗎?你是否考慮過安全? – 2012-04-03 17:37:34

+0

嗯,對不起,我還在學習,你有更好的主意嗎? – OnlyHope 2012-04-03 17:39:57

+1

是的,使用HTTPS。另外建議使用POST動詞以避免在Web服務器日誌中獲取密碼。 – 2012-04-03 17:41:32

回答

1

您可以創建一個PageMethod的:

[WebMethod] 
public static bool MyMethod(string username, string password) 
{ 
    ... 
    return true; 
} 

,然後擺脫雙JSON字符串化和調用頁面方法:

var postData = JSON.stringify({ 
    "username": $("#username").val(), 
    "password": $("#password").val() 
}); 
$.ajax({ 
    type: "POST", 
    url: "LoginPageForLearn.aspx/MyMethod", 
    data: postData, 
    contentType: "application/json; charset=utf-8", 
    success: callbackfunction, 
    error: function (msg) { alert(msg); } 
}); 

,並在回調,你可以測試的結果頁方法:

function callbackfunction(result) { 
    if (result.d) { 
     alert('success'); 
    } 
} 
+0

非常感謝,請問您能解釋webmethod的用法嗎? – OnlyHope 2012-04-03 17:34:51

+1

它被稱爲PageMethod。它是一個用'[WebMethod]'屬性修飾的靜態方法,您可以在ASP.NET頁面中定義該屬性,並且可以使用AJAX調用調用該屬性並向其發送JSON請求。它基本上允許你公開一些服務器端功能到客戶端腳本。 – 2012-04-03 17:36:06

+0

啊哈,好的... 感謝百萬 現在就試試看看會發生什麼:) – OnlyHope 2012-04-03 17:54:22

0

它不工作的方式。您還必須返回一些內容,以通知客戶端身份驗證成功或失敗。你必須考慮程序化的工作流程。

您可以在您調用的頁面中創建一個靜態方法,以Bool或JSON的形式返回一個值。沿着這些路線的東西:

[WebMethod] 
[ScriptMethod(UseHttpGet = true)] 
public static bool Authenticate JsonMethod(int username, string password) 
{ 
    bool isAuthenticated = // some method to authenticate 
    return isAuthenticated; 
} 

你的AJAX會是這個樣子:

$.ajax({ 
    type: "GET", 
    url: "http://localhost:49317/LoginPageForLearn.aspx/Authenticate", 
    data: postData, 
    contentType: "application/json; charset=utf-8", 
    dataType: "jsonp" 
}); 

在你的成功或失敗的回調,你會評估從那裏,你想用回做什麼值。

在任何情況下,處理程序可能會更好,因爲它不包含不必要的標記。

+0

非常感謝丹尼斯..不好意思,如果你告訴我這個處理程序是如何工作的,會不會有可能? – OnlyHope 2012-04-03 17:55:43

+0

HttpHandlers只是終點,它響應Web請求。它包括一些IHttpHandler和IHttpModule接口。通用處理程序以.ashx擴展名結尾。 你可以在這裏閱讀更多 - http://msdn.microsoft.com/en-us/library/bb398986.aspx – 2012-04-03 18:12:42

+0

謝謝你很多。非常有用的信息:) – OnlyHope 2012-04-03 19:04:58