Пример #1
0
        public LDAPHelper(LDAPConnectionInfo Info)
        {
            var ldapDirectoryIdentifier = new LdapDirectoryIdentifier(Info.HostName, Info.PortNumber, true, false);
            var qualifiedUserName       = Info.UserNameInNode + "=" + Info.UserName + "," + Info.BindDn;
            var networkCredential       = new NetworkCredential(qualifiedUserName, Info.Password);

            ldapConnection = new LdapConnection(ldapDirectoryIdentifier, networkCredential)
            {
                AuthType = Info.AuthenticationType
            };

            ldapConnection.SessionOptions.ProtocolVersion = 3;
            if (Info.VerifyServerCertificate)
            {
                ldapConnection.SessionOptions.VerifyServerCertificate += (conn, cert) => { return(true); }
            }
            ;
            if (Info.ConnectionType == LDAPConnectionInfo.ConnType.StartTLS)
            {
                ldapConnection.SessionOptions.StartTransportLayerSecurity(null);
            }

            ldapConnection.Bind();

            this.searchBaseDN = Info.BindDn;
            this.pageSize     = Info.PageSize;
        }
Пример #2
0
        private void ConnectViaLDAP()
        {
            try
            {
                txtTestOutput.Text = "";
                txtDetail.Text     = "";

                LDAPConnectionInfo info = new LDAPConnectionInfo
                {
                    PortNumber         = Convert.ToInt32(txtPortNum.Text),
                    BindDn             = txtBaseDn.Text,
                    HostName           = @txtHost.Text,
                    UserName           = txtUserCn.Text,
                    ConnectionType     = chkStartTLS.Checked ? LDAPConnectionInfo.ConnType.StartTLS : LDAPConnectionInfo.ConnType.LDAPS,
                    UserNameInNode     = txtIdentifier.Text,
                    Password           = SecureTheString(txtUserPassword.Text),
                    PageSize           = 1000,
                    AuthenticationType = AuthType.Basic,
                    //info.VerifiyDevCertificate = Convert.ToBoolean(ConfigurationManager.AppSettings.Get("verifyDevLDAPCertificate"));
                    VerifyServerCertificate = chkCertificate.Checked
                };

                Cursor.Current = Cursors.WaitCursor;

                txtTestOutput.Text += "Connecting to Server as " + info.UserName + (info.ConnectionType == LDAPConnectionInfo.ConnType.StartTLS ? " using StartTLS" : " using LDAPS") + " to " + info.HostName + "...." + Environment.NewLine;
                var openLDAPHelper = new LDAPHelper(info);
                txtTestOutput.Text += Environment.NewLine + "Connection Successful!" + Environment.NewLine + Environment.NewLine;

                txtTestOutput.Text += "--------------------------" + Environment.NewLine;
                txtTestOutput.Text += "SSL information:" + Environment.NewLine + "Cipher strength: " + openLDAPHelper.ldapConnection.SessionOptions.SslInformation.CipherStrength.ToString() + Environment.NewLine;
                txtTestOutput.Text += "Exchange strength: " + openLDAPHelper.ldapConnection.SessionOptions.SslInformation.ExchangeStrength.ToString() + Environment.NewLine;
                txtTestOutput.Text += "Protocol: " + openLDAPHelper.ldapConnection.SessionOptions.SslInformation.Protocol.ToString() + Environment.NewLine;
                txtTestOutput.Text += "Hash Strength: " + openLDAPHelper.ldapConnection.SessionOptions.SslInformation.HashStrength.ToString() + Environment.NewLine;
                txtTestOutput.Text += "Algorithm: " + openLDAPHelper.ldapConnection.SessionOptions.SslInformation.AlgorithmIdentifier.ToString() + Environment.NewLine;
                txtTestOutput.Text += "--------------------------" + Environment.NewLine;

                txtTestOutput.Text += "Searching in " + info.BindDn + " for " + txtAttributesToLoad.Text + "..." + Environment.NewLine;

                //Look up user information from LDAP for display on the web page, like "Welcome Tom Patterson"
                var searchFilter     = info.UserNameInNode + "=" + info.UserName;
                var attributesToLoad = txtAttributesToLoad.Text.Split(',');

                var searchResults = openLDAPHelper.GetUserInfo(searchFilter, attributesToLoad);

                foreach (var searchResultEntry in searchResults)
                {
                    foreach (SearchResultEntry searchResult in searchResultEntry)
                    {
                        foreach (var attribute in attributesToLoad)
                        {
                            if (searchResult.Attributes[attribute] != null)
                            {
                                txtDetail.Text += searchResult.Attributes[attribute].Name + ": " + searchResult.Attributes[attribute][0].ToString() + Environment.NewLine;
                            }
                        }
                        txtDetail.Text += Environment.NewLine;
                    }
                }

                txtTestOutput.Text += Environment.NewLine + "Search Complete.  Results below. " + Environment.NewLine;

                Cursor.Current = Cursors.Default;
            }
            catch (LdapException ldex)
            {
                Cursor.Current      = Cursors.Default;
                txtTestOutput.Text += " - Fail! \r\n\r\n" + "\r\nUnable to login:\r\n\t" + ldex.Message + "\r\n\r\n" + ldex.ToString();
            }
            catch (Exception ex)
            {
                Cursor.Current      = Cursors.Default;
                txtTestOutput.Text += " - Fail! \r\n\r\n" + "\r\nUnexpected exception occured:\r\n\t" + ex.GetType() + ":" + ex.Message + "\r\n\r\n" + ex.ToString();
            }
        }