title: Authenticating MSSQL through django-pyodbc using Windows Authentication from IIS7
body: <p>I'm working on building the intranet for my employer, and I've decided to try getting a django server setup. We use a windows server with MSSQL 2008 containing the bulk of our data. I'm looking to integrate the intranet with both the domain for authentication and the existing MSSQL server.</p>
  <p>Everything seems to be working up to the connection with the MSSQL server. The issue seems simple enough: it is trying to use the wrong credentials. I have IIS set up to use windows authentication and disabled the Anonymous login. When I go to the website, I am prompted to enter my credentials. When the page tries to load, django throws an error with the exception:</p>
  <pre><code>Exception Type: Error
  Exception Value: ('28000', &quot;[28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'DOMAIN\\DJANGO$'. (18456) (SQLDriverConnect)&quot;)
  Exception Location: c:\python35\lib\site-packages\sql_server\pyodbc\base.py in get_new_connection, line 301
  </code></pre>
  <p>Note that DJANGO is the name of the virtual computer I'm using to test this server. It seems the solution is as simple as figuring out how to pass the windows login credentials from IIS through django for the initial MSSQL connection.</p>
  <p>Now for some details, I have:</p>
  <ul>
  <li>IIS 7.5 running on a windows 7 Professional virtual machine for initial development</li>
  <li>python 3.5.1</li>
  <li>django 1.9.6</li>
  <li><a href="https://pypi.python.org/pypi/wfastcgi" rel="nofollow noreferrer">wfastcgi 2.2</a></li>
  <li><a href="https://pypi.python.org/pypi/django-pyodbc-azure" rel="nofollow noreferrer">django-pyodbc-azure 1.9.3.0</a></li>
  </ul>
  <p>I have the django settings.py entry:</p>
  <pre><code>...
  MIDDLEWARE_CLASSES = [
    ...
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.PersistentRemoteUserMiddelware',
    ...
  ]
  ...
  WSGI_APPLICATION = 'intranet.wsgi.application' #intranet is the project name
  ...
  DATABASES = {
    'default': {
      'ENGINE': 'sql_server.pyodbc',
      'NAME': 'DatabaseName',
      'HOST': 'Server',
      'AUTOCOMMIT': False,
    }
  }
  ...
  AUTHENTICATION_BACKENDS = [
    'django.contrib.auth.backends.RemoteUserBackend',
  ]
  ...
  </code></pre>
  <p>I have the interaction between IIS and django working based on the tutorial from <a href="https://pypi.python.org/pypi/wfastcgi" rel="nofollow noreferrer">https://pypi.python.org/pypi/wfastcgi</a>, with the WSIG_HANDLER changed by the suggestion of <a href="https://stackoverflow.com/questions/34840354/django-on-iis-django-core-exceptions-appregistrynotready-apps-arent-loaded-ye">Django on IIS: django.core.exceptions.AppRegistryNotReady: Apps aren&#39;t loaded yet</a> (change prompted because intranet did not have a module named wsgi_app(), not sure how I came to the idea to try it, but it worked and I moved on).</p>
  <p>It has taken much effort to get this far, hopefully this is an easy fix. My other thought was to abandon the django sql model system and deal with direct queries, but I'd rather do it the 'right' way before I redevelop the wheel.</p>