2017-09-26 26 views
0

我正在編寫一個解密程序,業務要求是兩個用戶必須登錄才能使其工作。我如何編寫在C#中試圖將兩個用戶登錄到SQL的程序?用戶是SQL用戶,我無法弄清楚如何在C#中驗證SQL用戶。如何使用SQL Server和C一次驗證兩個用戶

我想象了一個帶有兩個用戶名插槽和兩個密碼插槽並共享登錄按鈕的登錄屏幕。點擊後,每個用戶都會進行用戶驗證。如果兩個驗證都成功,則解密該文件。

任何幫助,建議或方向將不勝感激。

+2

一個想法是使用TPL,爲每個用戶憑證創建一個任務,並在不同線程中立即運行它們 https://msdn.microsoft.com/en-us/library/system.threading.tasks.task(v = vs.110).aspx – rmjoia

+0

我不知道那是什麼,但我會研究一些:) – Missy

+1

你是否正在尋找這個工作,以在兩個用戶在同一臺​​機器或遠程到每其他? –

回答

1

認證用戶的方法將是這個樣子 - 注意,連接字符串需要進行修改,在catch語句的具體應用,並在MessageBox可能是太多了,對於普通用戶:)

private bool AuthenticateUser() 
    { 
     // rather than picking up variable names, I am picking up the information from the text boxes on the screen. 
     bool retVal = false; 
     string cs1 = "Data Source=yourdatasource;Initial Catalog=yourcat;Persist Security Info=True;User ID=" + txtLogin1.Text.Trim() + ";Password=" + txtPW1.Text.Trim() + ";"; 
     string cs2 = "Data Source=yourdatasource;Initial Catalog=yourcat;Persist Security Info=True;User ID=" + txtLogin2.Text.Trim() + ";Password=" + txtPW2.Text.Trim() + ";"; 
     try 
     { 
      using (var connection = new SqlConnection(cs1)) 
      { 
       connection.Open(); 
       retVal = true; 
      } 
     } 
     catch (SqlException e2) 
     { 
      MessageBox.Show("User 1 Failed " + e2.ToString()); 
     } 

     try 
     { 
      using (var connection = new SqlConnection(cs2)) 
      { 
       connection.Open(); 
       retVal = true; 
      } 
     } 
     catch (SqlException ex) 
     { 
      MessageBox.Show("User 2 Failed "+ex.ToString()); 

     } 
     if (retVal) 
      { MessageBox.Show("Passed"); } 
     else 
      { MessageBox.Show("Please Try"); } 

     return retVal; 
    } 
} 
3

這樣的事情可以做的伎倆:

private async Task TestUserAuthAsync(){ 

    var task1 = Task.Run(AuthenticateUser("username1", "password1")); 
    var task2 = Task.Run(AuthenticateUser("username2", "password2")); 

    await Task.WhenAll(task1, task2); 
} 

你可以做更多,像迴歸結果日誌...

喜歡的東西Parallel.ForEach也能正常工作。

+0

是AuthenticateUser內建的SQL函數嗎? – Missy

+0

不,我正在編造一些方法。你需要編寫代碼..也許嘗試使用LINQ to sql ..我認爲你已經有了驗證一個用戶的代碼 – rmjoia

+0

他們想要使用你在SQL中設置的用戶。仍然提高你的幫助:) – Missy

相關問題