[英]Asp.net/JQuery: Set session after successful login
我正在使用 JQuery 對 SQL 數據庫進行身份驗證的登錄頁面。 我正在使用 ASP.NET WebMethod 來驗證用戶並返回 1 或 0,其中 1 表示成功,0 表示失敗。 如果成功,我想創建一個 session 並設置一個密鑰,但我得到一個 Object Null 異常錯誤(我標記了下面的行)。
登錄頁面非常簡單。 它接收用戶名/pin 並在按下按鈕時調用 ValidateUser。
我嘗試在谷歌上搜索並找到一些示例並嘗試復制它們,但我仍然得到相同的答案。
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public int ValidateUser(string user_id, string pin_nbr)
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[conName].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("ValidateUser", con);
try
{
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.Add("@UserID", SqlDbType.VarChar,100).Value = user_id;
da.SelectCommand.Parameters.Add("@PinNbr", SqlDbType.VarChar,100).Value = pin_nbr;
da.Fill(dt);
}
catch (Exception ex)
{
WriteToLog(errorMessage.ToString());
}
finally
{
da.Dispose();
}
if (dt.Rows.Count == 1)
{
Session["UserID"] = user_id; //<-- OBJECT NULL EXECPTION HERE
return 1;
}
return 0;
}
嘗試將 EnableSession = true 添加到您的 WebMethod 屬性。
[WebMethod(EnableSession = true)]
這應該可以解決問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.