簡體   English   中英

在C#中等於true

[英]equals in c# is true

我在用C#做webmethod。 在調試時,

(chk.Equals(oldpass))

查詢在左側和右側顯示相同的值。

但是仍然,執行不是移到if里面,而是移到顯示return語句的其他部分。 傻瓜。 是我的代碼。

[WebMethod (Description="for change in password")]
public string update(string authenid,string oldpass,string newpass)
{
     SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\Workspace\\visual studio workspace\\Tmrepo\\App_Data\\tmrepo.mdf;Integrated Security=True;User Instance=True");

    try
    {
        conn.Open();

  string chk = "select pwd from client where authenid = '"+ @authenid +"' ";
        if(chk.Equals(oldpass))
        {
            string update = "update client set pwd=@newpass where pwd=@oldpass and authenid=@authenid";
            SqlCommand cmd = new SqlCommand(update, conn);
            cmd.Connection = conn;
            cmd.Parameters.AddWithValue("@authenid", authenid);
            cmd.Parameters.AddWithValue("@oldpass", oldpass);
            cmd.Parameters.AddWithValue("@newpass", newpass);
            cmd.ExecuteNonQuery();

        }

        else
        {
            return "invalid oldpass";
        }
 conn.Close();
        return newpass;
    }
    catch (Exception ex)
    {
        return ex.ToString();
    }

}

這段代碼有什么問題嗎? 我是新手。 謝謝。

您尚未執行命令:您的舊密碼chk為:“從authenid ='“ + @authenid +”'“;從客戶端選擇pwd;這是不太可能的密碼。 例如,查看ExecuteScalar。

其他想法:

  • 參數化sql-不要在其中串聯id; 它應該是"select pwd from client where authenid = @authenid"; 在其中添加一個名為參數authenid從價值authenid 您可以在第二個ADO.NET查詢中正確地做到這一點。
  • 密碼應加鹽和散列:不直接存儲; 您將永遠無法提取和/或解密密碼

(外部編輯)

更新 :執行代碼為"select pwd from client where authenid = '"+ @authenid +"' "; 因為"select pwd from client where authenid = '@authenid' "; 返回空值。

update2cmd.ExecuteScalar(); 使它工作。 並刪除cmd.ExecuteNonQuery();

您的代碼所做的是將select pwd from client where authenid = some_value的值進行比較,這將是錯誤的!

固定代碼邏輯:

        string oldpass = "somehing";

        string authenid = "pass_to_test";
        string sql = string.Format("select pwd from client where authenid = '{0}' ", authenid);
        string chk = null;
        SqlCommand cmd = new SqlCommand(update, conn);

        var reader = cmd.ExecuteReader();
        if (reader.Read())
        {
            // has record with username
            chk = reader.GetString(0);
            if (chk.Equals(oldpass))
            {
                string update = "update client set pwd=@newpass where pwd=@oldpass and authenid=@authenid";

                cmd.CommandText = update;
                cmd.Connection = conn;
                cmd.Parameters.AddWithValue("@authenid", authenid);
                cmd.Parameters.AddWithValue("@oldpass", oldpass);
                cmd.Parameters.AddWithValue("@newpass", newpass);
                cmd.ExecuteNonQuery();

            }

            else
            {
                return "invalid oldpass";
            }

        }
        else
        {
            // not a valid username
        }
        reader.Close();
        reader.Dispose();

終於解決了!!! 我使用數據集。 具有完全正確代碼的第二種方法是:

string chk = "select pwd from client where authenid = @authenid";
        SqlCommand cmd1 = new SqlCommand(chk , conn);
        cmd1.Parameters.AddWithValue("@authenid", authenid);
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(cmd1);
        da.Fill(ds);
        int cnt = ds.Tables[0].Rows.Count;
        if (cnt > 0)
        {
            if (ds.Tables[0].Rows[0]["pwd"].ToString().Equals(oldpass))
            {
                string update = "update client set pwd=@newpass where pwd=@oldpass and authenid=@authenid";
                SqlCommand cmd = new SqlCommand(update, conn);
                cmd.Connection = conn;
                cmd.Parameters.AddWithValue("@authenid", authenid);
                cmd.Parameters.AddWithValue("@oldpass", oldpass);
                cmd.Parameters.AddWithValue("@newpass", newpass);
                cmd.ExecuteNonQuery();
            }            
        }

希望它對像我這樣的新手有幫助!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM