title: Using oracle DBMS_LDAP to validate user credentials against Microsoft Active Directory
body: <p>We are trying to authenticate our Application users using dbms_ldap against Microsoft AD and we have no means of testing it in our offshore environment</p>
  
  <p>We have three specific questions </p>
  
  <p>1)How to verify the user exists in Microsoft Active directory</p>
  
  <p>We use the below code to get the Distinguished name of the Application user</p>
  
  <pre><code>DBMS_LDAP.USE_EXCEPTION := FALSE;
  retval := DBMS_LDAP.search_s(ld       =&gt; ldapSession,
                             base     =&gt; LDAP_BASE,
                             scope    =&gt; DBMS_LDAP.SCOPE_SUBTREE,
                             filter   =&gt; '(&amp;(objectclass=USER)(SAMAccountName=' ||
                                         p_username || '))',
                             attrs    =&gt; attrList,
                             attronly =&gt; 0,
                             res      =&gt; ldapMessage);
    -- get the DN
  if retval &lt;&gt; DBMS_LDAP_UTL.SUCCESS THEN
  RAISE l_ldap_exception;
  END IF;
  userDN := DBMS_LDAP.get_dn(ldapSession, ldapMessage);
  </code></pre>
  
  <p>So the first question is what will be the value of </p>
  
  <pre><code> userDN and ldapMessage
  </code></pre>
  
  <p>if the user doesn't exist in Microsoft AD</p>
  
  <p>2)Suppose the user exists and has entered the wrong password in that case what will be the return value of retval</p>
  
  <pre><code>if p_password is null then
  raise_application_error(-20000, 'Invalid Null password');
  else
  retval := DBMS_LDAP.simple_bind_s(ldapSession,userDN, p_password);
  end if;
  
  if retval &lt;&gt; DBMS_LDAP_UTL.SUCCESS THEN
  RAISE l_ldap_exception;
  and if;
  </code></pre>
  
  <p>3)My third question is suppose the user has logged in the system and ldapsession is still not unbind what will be the way to identify duplicate session</p>