2016-08-26 92 views
0

在我的網站我想允許用戶從電子郵件或用戶名登錄。我嘗試以下,但是當我去的電子郵件不具有的用戶名是唯一的登錄允許用戶同時使用userename或電子郵件登錄

if (!string.IsNullOrEmpty(username.Text) & !string.IsNullOrEmpty(password.Text)) { 
    string str = "select * from login where [email protected] or email = @email and [email protected]"; 
    MySqlCommand cmd = new MySqlCommand(str, con); 
    cmd.Parameters.AddWithValue("@username", username.Text); 
    cmd.Parameters.AddWithValue("@email", username.Text); 
    cmd.Parameters.AddWithValue("@password", password.Text); 
    con.Open(); 
    MySqlDataAdapter da = new MySqlDataAdapter(cmd); 
    DataSet ds = new DataSet(); 
    da.Fill(ds); 
    con.Close(); 
    user = ds.Tables(0).Rows(0)("username"); 
    email = ds.Tables(0).Rows(0)("email"); 
    pswd = ds.Tables(0).Rows(0)("password"); 
    page = ds.Tables(0).Rows(0)("link"); 
    if (ds.Tables(0).Rows.Count > 0) { 
     if (user == username.Text.ToString | email == username.Text.ToString & pswd == password.Text.ToString) { 
      Response.Cookies("User_Type").Value = "manager"; 
      Response.Cookies("masterLogin").Value = username.Text; 
      Response.Redirect(page); 
     } else { 
      Response.Write("<script language='javascript'>alert('User name or password is invalid');</script>"); 
     } 
    } 
} 
+4

你存儲明文密碼來代替你的代碼?不要這樣做。 –

+0

爲什麼不使用[asp.net認證](https://msdn.microsoft.com/en-us/library/eeyk640h.aspx)。它負責密碼加密,用戶鎖定等等。 – VDWWD

回答

1

檢查這個答案就在嘗試用下面的代碼

if (user == username.Text.ToString || email == username.Text.ToString 
 
     && pswd == password.Text.ToString) {

0

的缺陷是在這一行:

if (user == username.Text.ToString | email == username.Text.ToString 
    & pswd == password.Text.ToString) { 

代替|&,你需要寫||&&

另外,您應該將||置於括號內,因爲&&具有更高的優先級。

所以這是解決方案:

if ((user == username.Text.ToString || email == username.Text.ToString) 
    && pswd == password.Text.ToString) { 
+0

我調試了我的代碼,我在這行看不到任何錯誤。我的查詢生成的空結果創建問題 – SUN

+0

@SUN單用|和&一般是錯誤的。這些是位操作員。你應該總是使用布爾運算符||和&&布爾操作。 –

0

你或你哪裏會短你查查看。

嘗試例如下面這相當於你的邏輯:
SELECT 'a' WHERE 1 = 1 OR 2 = 1 AND 3 = 1

這將選擇'a'
什麼你以後可能:

select * from login where ([email protected] or email = @email) and [email protected]"

0

你應該考慮分組在你的查詢中通過加上它們的狀態就好像

select * 
from login 
where ([email protected] or email = @email) 
and [email protected]"; 
+0

我在mysql中試過這個查詢。這是我的實際輸入(select * from login where(username ='09840610866'或email ='09840610866')和Password ='subhash123'),但給出空的結果 – SUN

1

使用下面的sql查詢。

select * from login where ([email protected] or email = @email) and [Password][email protected] // Password is a reserved keyword you cant use this as a column name. 

並請指出您正在得到什麼錯誤。

相關問題