2017-04-23 70 views
0

如何使用Asp.Net從MySql數據庫檢索鹽?如何使用Asp.net從MySql數據庫獲取鹽?

我想使用檢索到的鹽添加到用戶輸入的密碼來生成SHA256哈希,然後驗證用戶。

這裏就是我試圖做的,以獲取鹽:

String userNameEntered = UserN_TextBox.Text; 
String passwordEntered = Password_TextBox.Text; 
String connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString(); 
connection = new MySqlConnection(connectionString); 
connection.Open(); 
MessageBox.Show("Successfully connected to database"); 
String queryString = "select salt from xyz.abc_table where salt = @Salt"; 
command = new MySqlCommand(queryString, connection); 
command.Parameters.AddWithValue("@Salt", queryString); 
reader = command.ExecuteReader(); 
Response.Write("Salt retrived is" + reader); 
reader.Close(); 
connection.Close(); 

當我執行這段代碼,它返回MySQL的數據讀取器庫,而不是在數據庫中的鹽....

在此先感謝... :)

+0

您能添加一個問題描述嗎?你已經描述了你想要做什麼,你已經展示了一些代碼---但問題在哪裏? – ventiseis

+0

我已經更新了這個問題。如果你知道任何事,請告訴我。謝謝...!!! – Deep

+0

[從sql reader c#獲取值]的可能重複(http://stackoverflow.com/questions/27874566/getting-values-from-sql-reader-c-sharp) – ventiseis

回答

0

在這裏,我已經解決了我的問題。這是解決問題的辦法。它可能會幫助某人:

try 
     { 

      String connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString(); 
      connection = new MySqlConnection(connectionString); 
      connection.Open(); 
      MessageBox.Show("Successfully connected to database"); 
      String queryString = "select salt from xyz.abc_table where email_address = @E_Address"; 
      command = new MySqlCommand(queryString, connection); 
      command.Parameters.AddWithValue("@E_Address", UserN_TextBox.Text); 
      reader = command.ExecuteReader(); 
      if (reader.Read()) 
      { 
       Response.Write("Retrived Salt is " + reader["salt"]); 
       reader.Close(); 
       connection.Close(); 
      } 

     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Failed due to" +ex); 
     } 
相關問題