2

我所擁有的功能是允許域用戶對其LDAP憑證進行身份驗證。然而,當我將一個已知的密碼硬編碼爲一個原始字符串時,它的工作很長......當然,這是一個不行的密碼。我希望傳入從我設置的TextBox收到的字符串值。下面是函數:將字符串變量傳入用於口令爭用的Nework證書構造函數

public static bool fnValLDAPCreds() 
    { 
     bool validation;         

     try 
     {     

      LdapConnection ADConn = new LdapConnection(new LdapDirectoryIdentifier((string)null, false, false)); 
      NetworkCredential NetCred = new NetworkCredential(Environment.UserName, "Password123", Environment.UserDomainName); 

      ADConn.Credential = NetCred; 
      ADConn.AuthType = AuthType.Negotiate; 
      // the user's authenticated here; creds used to login on the domain controller. 

      ADConn.Bind(NetCred); 
      validation = true; 
      MessageBox.Show("You were successfully authenticated against AD using LDAP!"); 
     } 

     catch (LdapException) 
     { 
      validation = false; 
      MessageBox.Show("Your login was unsuccesful. Try a different set of credentials."); 
     } 

     return validation; 
    } 

什麼我試圖從我TextBox做一個值是替補,但由於它位於static bool我都沒有成功作出任何外部引用的一個控制當前上下文。我在按鈕處理函數中調用這個函數來關閉它。我怎樣才能換一個string DomPassWord變量,從我設置的文本框中獲取它的值來獲取它?

NetworkCredential NetCred = new NetworkCredential(Environment.UserName, DomPassWord, Environment.UserDomainName);是我努力的工作,因爲我可以使用類似DomPassWord = txtUserPW.Text的東西在域中安全地匹配密碼而無需硬編碼。嘗試了SecureString路線,但在這方面也沒有成功。有任何想法嗎?

+2

爲什麼不給fnValLDAPCreds方法添加一個'string password'參數,用''password'替換''Password123'',並將其稱爲'fnValLDAPCreds(myTextBox.Text)'?或者你的問題不是很清楚 –

+0

我很難看到更大的圖片。你是否這樣做,以確保登錄爲'bob'的用戶實際上是bob,通過讓他重新輸入他的密碼?或者你是否在其他地方用它作爲證據來完成證書? – Kevin

回答

5

你不能訪問靜態方法中的文本框,因爲它們不是靜態字段(至少它看起來像你寫的內容)。

但是,您可以簡單地將您的參數傳遞給您的方法。改變它是這樣的:

public void ButtonClick(object sender, EventArgs args) 
{ 
    // bool valid = fnValLDAPCreds(Environment.UserName, "Password123", Environment.UserDomainName); 
    bool valid = fnValLDAPCreds(txtUserName.Text, txtUserPW.Text, Environment.UserDomainName); 
} 

public static bool fnValLDAPCreds(string username, string password, string domain) 
{ 
    try 
    { 
     LdapConnection ADConn = new LdapConnection(new LdapDirectoryIdentifier((string)null, false, false)); 
     NetworkCredential NetCred = new NetworkCredential(username, password, domain); 

     ADConn.Credential = NetCred; 
     ADConn.AuthType = AuthType.Negotiate; 
     // the user's authenticated here; creds used to login on the domain controller. 

     ADConn.Bind(NetCred); 
     MessageBox.Show("You were successfully authenticated against AD using LDAP!"); 
     return true; 
    } 
    catch (LdapException) 
    { 
     MessageBox.Show("Your login was unsuccesful. Try a different set of credentials."); 
     return false; 
    } 
} 
+0

感謝每一位具有良好洞察力的人,我希望我可以分享賞金,但這是現場。目標如前所述......確保查理真的是「查理」......查理是知道他的域名ID和域密碼的人。 – DesignerMind

+0

爲了進一步闡述,我不希望NTLM通過基於正確用戶名的信用傳遞。如果查理更改爲一個荒謬的密碼字符串,他將需要輸入該字符串進行身份驗證。我知道在這一點上我可以通過'SecureString'加密密碼,甚至在盒子上設置'UseSystemPasswordChar',但是在字符串傳遞功能上模糊了! – DesignerMind

0

有點切線,但你有沒有想過AuthType.Ntlm?如果你這樣做的目的是通過讓用戶輸入他的密碼來確保用戶'Charlie'實際上是Charlie?那麼你在正確的軌道上。但是,如果您嘗試使用當前用戶憑據連接到AD,以此作爲獲取AD本身的方式?然後,你可能想看看

ADConn.AuthType = AuthType.Ntlm; 

...並讓Windows處理這件事情(無需在用戶輸入密碼類型在所有 - 它會使用當前Windows憑據)

相關問題