Compare() public method

Asynchronously compares an attribute value with one in the directory, using the specified queue. Please note that a successful completion of this command results in one of two status codes: LdapException.COMPARE_TRUE if the entry has the value, and LdapException.COMPARE_FALSE if the entry does not have the value or the attribute.
LdapException A general exception which includes an error /// message and an Ldap error code. /// ///
public Compare ( System dn, LdapAttribute attr, LdapResponseQueue queue ) : LdapResponseQueue
dn System The distinguished name of the entry containing an /// attribute to compare. /// ///
attr LdapAttribute An attribute to compare. /// ///
queue LdapResponseQueue The queue for messages returned from a server in /// response to this request. If it is null, a /// queue object is created internally. /// ///
return LdapResponseQueue
Example #1
0
    public static void Main(System.String[] args)
    {
        if (args.Length != 5)
        {
            System.Console.Out.WriteLine("Usage:   mono VerifyPassword <host name>" + " <login dn> <password> <object dn>\n" + "         <test password>");
            System.Console.Out.WriteLine("Example: mono VerifyPassword Acme.com " + "\"cn=Admin,o=Acme\" secret\n" + "         \"cn=JSmith,ou=Sales,o=Acme\" testPassword");
            System.Environment.Exit(0);
        }

        int ldapPort = LdapConnection.DEFAULT_PORT;
        int ldapVersion = LdapConnection.Ldap_V3;
        System.String ldapHost = args[0];
        System.String loginDN = args[1];
        System.String password = args[2];
        System.String objectDN = args[3];
        System.String testPassword = args[4];
        LdapConnection conn = new LdapConnection();

        try
        {
            // connect to the server
            conn.Connect(ldapHost, ldapPort);

            // authenticate to the server
            conn.Bind(ldapVersion, loginDN, password);

            LdapAttribute attr = new LdapAttribute("userPassword", testPassword);
            bool correct = conn.Compare(objectDN, attr);

            System.Console.Out.WriteLine(correct?"The password is correct.":"The password is incorrect.\n");

            // disconnect with the server
            conn.Disconnect();
        }
        catch (LdapException e)
        {
            if (e.ResultCode == LdapException.NO_SUCH_OBJECT)
            {
                System.Console.Error.WriteLine("Error: No such entry");
            }
            else if (e.ResultCode == LdapException.NO_SUCH_ATTRIBUTE)
            {
                System.Console.Error.WriteLine("Error: No such attribute");
            }
            else
            {
                System.Console.Error.WriteLine("Error: " + e.ToString());
            }
        }
        catch (System.IO.IOException e)
        {
            System.Console.Out.WriteLine("Error: " + e.ToString());
        }
        System.Environment.Exit(0);
    }
Example #2
0
    public static void Main( String[] args )
    {
        if (args.Length != 4)
        {
            Console.Error.WriteLine("Usage:   mono CompareAttrs <host name> <login dn> "
                + "<password> <compare dn> ");
            Console.Error.WriteLine("Example: mono CompareAttrs Acme.com \"cn=Admin,"
                + "o=Acme\" secret\n         \"cn=JSmith,ou=Sales,o=Acme\"");
            Environment.Exit(1);
        }

        int ldapPort = LdapConnection.DEFAULT_PORT;
        int ldapVersion = LdapConnection.Ldap_V3;
        bool compareResults = false;
        String ldapHost = args[0];
        String loginDN  = args[1];
        String password = args[2];
        String dn = args[3];
        LdapConnection lc = new LdapConnection();
        LdapAttribute attr = null;

        try
        {
            // connect to the server
            lc.Connect( ldapHost, ldapPort );

            // authenticate to the server
            lc.Bind( ldapVersion, loginDN, password );

            attr =new LdapAttribute( "objectclass", "inetOrgPerson" );
            System.Collections.IEnumerator allValues = attr.StringValues;
            allValues.MoveNext();
            // Compare the value of the objectclass attribute.
            if ( compareResults == lc.Compare(dn, attr))
                Console.WriteLine("\t" + (String)allValues.Current
                           + " is contained in the " + attr.Name + " attribute." );
            else
                Console.WriteLine("\t" + (String)allValues.Current
                           + " is not contained in the " + attr.Name + " attribute." );

            attr = new LdapAttribute( "sn", "Bunny" );
            allValues = attr.StringValues;
            allValues.MoveNext();

            // Compare the value of the sn attribute.
            if ( compareResults == lc.Compare(dn, attr))
                Console.WriteLine("\t" + (String)allValues.Current
                           + " is contained in the " + attr.Name + " attribute." );
            else
                Console.WriteLine("\t" + (String)allValues.Current
                           + " is not contained in the " + attr.Name + " attribute." );

            // disconnect with the server
            lc.Disconnect();
        }
        catch( LdapException e )
        {
            Console.WriteLine( "Error: " + e.ToString() );
        }
        catch( Exception e )
        {
            Console.WriteLine( "Error: " + e.ToString() );
        }
        Environment.Exit(0);
    }