title: Django Auth LDAP - Direct Bind using sAMAccountName
body: <p>There are two ways to authenticate a user using Django Auth LDAP</p>
  
  <ol>
  <li>Search/Bind and  </li>
  <li>Direct Bind.</li>
  </ol>
  
  <p>The first one involves connecting to the LDAP server either anonymously or with a fixed account and searching for the distinguished name of the authenticating user. Then we can attempt to bind again with the user’s password. </p>
  
  <p>The second method is to derive the user’s DN from his username and attempt to bind as the user directly.</p>
  
  <p>I want to be able to do a direct bind using the userid (sAMAccountName) and password of the user who is trying to gain access to the application. Please let me know if there is a way to achieve this? At the moment, I cannot seem to make this work due to the problem explained below.</p>
  
  <p>In my case, the DN of users in LDAP is of the following format</p>
  
  <pre><code>**'CN=Steven Jones,OU=Users,OU=Central,OU=US,DC=client,DC=corp'**
  </code></pre>
  
  <p>This basically translates to 'CN=FirstName LastName,OU=Users,OU=Central,OU=US,DC=client,DC=corp'</p>
  
  <p>This is preventing me from using Direct Bind as the <strong>sAMAccountName</strong> of the user is <strong>sjones</strong> and this is the parameter that corresponds to the user name (%user) and I can't figure out a way to frame a proper <strong>AUTH_LDAP_USER_DN_TEMPLATE</strong> to derive the User's DN using. </p>
  
  <p>Due to the above explained problem, I am using Search/Bind for now but this requires me to have a fixed user credential to be specified in <strong>AUTH_LDAP_BIND_DN</strong> and <strong>AUTH_LDAP_BIND_PASSWORD</strong>. </p>
  
  <p>Here is my current <strong>settings.py</strong> configuration</p>
  
  <pre><code>AUTH_LDAP_SERVER_URI = "ldap://10.5.120.161:389"
  AUTH_LDAP_BIND_DN='CN=Steven Jones,OU=Users,OU=Central,OU=US,DC=client,DC=corp'
  AUTH_LDAP_BIND_PASSWORD='fga.1234'
  #AUTH_LDAP_USER_DN_TEMPLATE = 'CN=%(user)s,OU=Appl Groups,OU=Central,OU=US,DC=client,DC=corp'
  AUTH_LDAP_USER_SEARCH = LDAPSearchUnion(
      LDAPSearch("OU=Users, OU=Central,OU=US,DC=client,DC=corp",ldap.SCOPE_SUBTREE, "(sAMAccountName=%(user)s)"),
      LDAPSearch("OU=Users,OU=Regional,OU=Locales,OU=US,DC=client,DC=corp",ldap.SCOPE_SUBTREE, "(sAMAccountName=%(user)s)"),
      )
  AUTH_LDAP_USER_ATTR_MAP = {"first_name": "givenName", "last_name": "sn","email":"mail"}
  AUTH_LDAP_GROUP_SEARCH = LDAPSearch("CN=GG_BusinessApp_US,OU=Appl Groups,OU=Central,OU=US,DC=client,DC=corp",ldap.SCOPE_SUBTREE, "(objectClass=groupOfNames)")
  AUTH_LDAP_GROUP_TYPE = GroupOfNamesType()
  AUTH_LDAP_REQUIRE_GROUP = 'CN=GG_BusinessApp_US,OU=Appl Groups,OU=Central,OU=US,DC=client,DC=corp'
  </code></pre>
  
  <p>Looking forward for some guidance from the wonderful folks in here.</p>