簡體   English   中英

比較SQL Server中同一表中的兩行

[英]Comparing two rows in the same table in sql server

我已經看過類似的答案,但這就是我想要的,因為我沒有從先前回答的問題中找到任何答案:

這是我的情況:我有一個包含username, password, key_pin的表[res_user] username, password, key_pin其中保存了用戶名,加密的密碼以及4位數字的密碼或密鑰。

我正在使用C#開發一個應用程序,該應用程序允許管理員登錄並從應用程序本身對數據庫進行更改。

首先會提示管理器使用用戶名,密碼及其提供的key_pin登錄。 密碼將用於加密和解密數據庫的密碼。

現在,我有一個用戶名[manager]和一個已加密的密碼,已經使用key_pin保存在數據庫中。

如何確保管理器登錄正確,這意味着我如何比較C#應用程序中數據庫中的用戶名和加密密碼。

這些是我將在應用程序本身中實現的步驟(使用c#中的SQL語法):

  1. 加密密碼,

  2. 使用登錄用戶名獲取數據庫中保存的加密,以及

  3. 比較加密,然后將是或否返回給應用以進行訪問控制。

允許5次嘗試登錄。

這是我做的第一部分和第二部分:

try
{
   using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["STRING"].ConnectionString))
   {
     using (SqlCommand cmd = new SqlCommand("dbo.Res_User", con))
     {
       cmd.CommandText = "INSERT INTO Res_User(username, password, key_pin) SELECT '" + username + "' , dbo.fnEncDecRc4('" + pin + "','" + password + "'), '" + pin + "'";
       con.Open();
       cmd.ExecuteNonQuery();

       MessageBox.Show("Added", "Information", MessageBoxButtons.OK);

       cmd.CommandText = "SELECT password FROM Res_User WHERE username = @username";
       cmd.Parameters.AddWithValue("@username", username);
       cmd.ExecuteNonQuery();

       using (SqlDataReader reader = cmd.ExecuteReader())
       {
           if (reader.HasRows)
           {
               while (reader.Read())
               {
                    MessageBox.Show(reader["password"].ToString(), "Information", MessageBoxButtons.OK);
               }
           }
       }

我該如何進行第三部分?

僅當有人可以幫助我比較保存的enc時。 密碼和登錄enc。 我在第一部分中所做的密碼。

讓我知道是否需要更多信息。

謝謝。

任何幫助將不勝感激。

在服務器上進行驗證時,可以嘗試重新加密密碼並將其固定在服務器上。 這樣您將調用您的加密函數,然后進行比較,如果有結果,您就知道輸入了正確的密碼。 修改您的代碼,如下所示:

try
{
   using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["STRING"].ConnectionString))
   {
     using (SqlCommand cmd = new SqlCommand("dbo.Res_User", con))
     {
       cmd.CommandText = "INSERT INTO Res_User(username, password, key_pin) SELECT '" + username + "' , dbo.fnEncDecRc4('" + pin + "','" + password + "'), '" + pin + "'";
       con.Open();
       cmd.ExecuteNonQuery();

       MessageBox.Show("Added", "Information", MessageBoxButtons.OK);

       cmd.CommandText = "SELECT password FROM Res_User WHERE username = @username AND key_pin = @pin AND password = dbo.fnEncDecRc4(@pin, @password)";
       cmd.Parameters.AddWithValue("@username", username);
       cmd.Parameters.AddWithValue("@pin", pin);
       cmd.Parameters.AddWithValue("@password", password);
       cmd.ExecuteNonQuery();

       using (SqlDataReader reader = cmd.ExecuteReader())
       {
           if (reader.HasRows)
           {
               //successfully validated.
           }
       }

暫無
暫無

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

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