2017-03-06 98 views
0

我的登錄窗口使用LDAP對用戶進行身份驗證。但是,在驗證時,它總是返回false。wpf - 驗證時LDAP始終返回false

下面是驗證代碼,我從CodeProject上了:

public bool fnValidateUser() 
    { 
     bool validation; 
     try 
     { 
      LdapConnection lcon = new LdapConnection 
        (new LdapDirectoryIdentifier((string)null, false, false)); 
      NetworkCredential nc = new NetworkCredential(Environment.UserName, 
            txtPassword.SecurePassword, Environment.UserDomainName); 
      lcon.Credential = nc; 
      lcon.AuthType = AuthType.Negotiate; 
      // user has authenticated at this point, 
      // as the credentials were used to login to the dc. 
      lcon.Bind(nc); 
      validation = true; 
     } 
     catch (LdapException) 
     { 
      validation = false; 
     } 
     return validation; 
    } 

txtPassword.SecurePassword是PasswordBox。當我輸入我的密碼/ PIN並點擊登錄時,只要驗證爲false,它就會顯示MessageBox。

我在做什麼錯?

UPDATE:的異常指示「的LDAP服務器不可用」,在這條線lcon.Bind(nc);

+0

添加一個日誌來捕捉異常,以及你作爲異常得到了什麼? – Dinesh

+0

@Dinesh我剛剛添加了它 – bruh1234

+0

根據此例外情況,您的LDAP服務器已關閉或根本沒有連接。 – Dinesh

回答

0

我繼續前進,發現了另一種方法,不使用LDAP。

PrincipalContext adContext = new PrincipalContext(ContextType.Machine); 
private async void btnLogin_Click(object sender, RoutedEventArgs e) 
    { 
     try 
     { 
      using (adContext) 
      { 
       if (adContext.ValidateCredentials(txtUsername.Text, txtPassword.Password)) 
       { 
        MainWindow main = new MainWindow(); 

        main.Show(); 
        main.txtLoggedInUser.Text = UserPrincipal.Current.DisplayName; 

        this.Close(); 
       } 
       else 
       { 
        MessageBox.Show("Incorrect Username or Password!"); 
       } 
      } 
     } 
     catch(Exception ex) 
     { 
      var exceptionDialog = new MessageDialog 
      { 
       Message = { Text = ex.ToString() } 
      }; 

      await DialogHost.Show(exceptionDialog, "RootDialog"); 
     } 
    } 
0

你可以試試這個代碼樣片。

// the username and password to authenticate 
const string domain = "OU=Organization,DC=mydomain,DC=com"; 
string password = "mypass"; 
string userName = "myuser"; 

// define your connection 
LdapConnection ldapConnection = new LdapConnection("ldap.mydomain.com:389"); 

try 
{ 
    // authenticate the username and password 
    using (ldapConnection) 
    { 
     // pass in the network creds, and the domain. 
     var networkCredential = new NetworkCredential(username, password, domain); 

     // if we're using unsecured port 389, set to false. If using port 636, set this to true. 
     ldapConnection.SessionOptions.SecureSocketLayer = false; 

     // since this is an internal application, just accept the certificate either way 
     ldapConnection.SessionOptions.VerifyServerCertificate += delegate { return true; }; 

     // to force NTLM\Kerberos use AuthType.Negotiate, for non-TLS and unsecured, just use AuthType.Basic 
     ldapConnection.AuthType = AuthType.Basic; 

     // authenticate the user 
     ldapConnection.Bind(networkCredential); 
    } 
    catch (LdapException ldapException) 
    { 
     //Authentication failed, exception will dictate why 
    } 
} 
+0

域和LdapConnection在用戶PC上安裝時需要動態。我應該用Environment.UserDomainName替換字符串嗎? – bruh1234

+0

嘗試硬編碼值,並確保工作正常,然後您可以將其替換爲動態值。 – Dinesh