Novell.Directory.Ldap.LdapConnection.startTLS C# (CSharp) Method

startTLS() public method

Starts Transport Layer Security (TLS) protocol on this connection to enable session privacy. This affects the LdapConnection object and all cloned objects. A socket factory that implements LdapTLSSocketFactory must be set on the connection.
LdapException Thrown if TLS cannot be started. If a /// SocketFactory has been specified that does not implement /// LdapTLSSocketFactory an LdapException is thrown. /// ///
public startTLS ( ) : void
return void
        public virtual void startTLS()
        {
            LdapMessage startTLS = MakeExtendedOperation(new LdapExtendedOperation(LdapConnection.START_TLS_OID, null), null);

            int tlsID = startTLS.MessageID;

            conn.acquireWriteSemaphore(tlsID);
            try
            {
                if (!conn.areMessagesComplete())
                {
                    throw new LdapLocalException(ExceptionMessages.OUTSTANDING_OPERATIONS, LdapException.OPERATIONS_ERROR);
                }
                // Stop reader when response to startTLS request received
                conn.stopReaderOnReply(tlsID);

                // send tls message
                LdapResponseQueue queue = SendRequestToServer(startTLS, defSearchCons.TimeLimit, null, null);

                LdapExtendedResponse response = (LdapExtendedResponse) queue.getResponse();
                response.chkResultCode();

                conn.startTLS();
            }
            finally
            {
                //Free this semaphore no matter what exceptions get thrown
                conn.startReader();
                conn.freeWriteSemaphore(tlsID);
               }
            return ;
        }

Usage Example

Example #1
0
        static void Main(string[] args)
        {
            if ( args.Length != 4)
            {
            Console.WriteLine("Usage:   mono StartTLS <host name> <ldap port>  <login dn>" + " <password>  ");
            Console.WriteLine("Example: mono StartTLS Acme.com 389"  + " \"cn=admin,o=Acme\"" + " secret  \n");
            Console.WriteLine("Import the server Trusted Root Certificate in Mono trust store using certmgr.exe utility e.g.\n");
                        Console.WriteLine("certmgr -add -c Trust /home/exports/TrustedRootCert.cer\n");

            return;
            }

            string ldapHost = args[0];
            int ldapPort = System.Convert.ToInt32(args[1]);
            String loginDN  = args[2];
            String password = args[3];
            try
            {
            LdapConnection conn= new LdapConnection();
            Console.WriteLine("Connecting to:" + ldapHost);
            conn.Connect(ldapHost,ldapPort);
            conn.startTLS();
            conn.Bind(loginDN,password);
            Console.WriteLine("TLS Bind Completed Successfull");
            conn.stopTLS();
            Console.WriteLine("Stop TLS Completed Successfull");
            conn.Disconnect();
            }
            catch(Exception e)
            {
            Console.WriteLine("Error:" + e.Message);
            }
        }