[英]login authentication logic not working in C#.NET
我創建了表user_info,並在其中有2個用戶名和密碼。 當我執行以下代碼時,即使我鍵入正確的用戶名和密碼,它也總是進入“其他”狀態。
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
string v = System.Configuration.ConfigurationManager.ConnectionStrings["harish"].ConnectionString;
con = new OracleConnection(v);
con.Open();
cmd = new OracleCommand("select * from user_info where username='" + Login1.UserName.Trim() + "' and password='" + Login1.Password + "'", con);
dr = cmd.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
Response.Redirect("Default2.aspx");
}
else
{
Response.Redirect("Default.aspx");
}
con.Close();
dr.Close();
}
它正在進入其他,因為博士沒有行。 要找出原因,請在
dr = cmd.ExecuteReader();
然后使用帶有該參數的sql select命令,並對該命令作為sql語句對數據庫運行,以查看其是否返回行。 我的懷疑是,無論您認為給Login1.Username和/或Login1.Password提供什么,它都不會傳遞任何東西,但很難說,而無需知道Login1是如何填充的。
順便說一句,如果這些是網頁上的文本框,則需要使用Login1.Username.Text和Login1.Password.Text來獲取文本框中的實際字符串。
首先,您至少應該對密碼進行哈希處理。 另外,最好的做法是不要將您的連接留在課堂上。 使用它們時,應該創建,打開和關閉它們。 與命令,讀取器等相同... 使用using塊可以非常容易地完成。
接下來,確保在使用Login1.UserName和Login1.Password時訪問的是實際的字符串值,而不是控件。 如果使用控件,則需要使用Login1.UserName.Text.Trim()和Login1.Password.Text.Trim()。 您可以通過將構建的查詢存儲到本地字符串值中並查看實際構建的內容來確保這一點。
不要將DataReader用於您正在做的事情。 而是使用ExecuteScalar方法:
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
string v = System.Configuration.ConfigurationManager.ConnectionStrings["harish"].ConnectionString;
con = new OracleConnection(v);
con.Open();
cmd = new OracleCommand("select * from user_info where username='" + Login1.UserName.Trim() + "' and password='" + Login1.Password + "'", con);
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count > 0)
{
Response.Redirect("Default2.aspx");
}
else
{
Response.Redirect("Default.aspx");
}
con.Close();
}
設置完成后,在if(count> 0)行上放置一個斷點。 檢查存儲在本地變量中的查詢,然后檢查計數。 這應該為您提供所需的一切。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.