[Tutor] Python and NT Authentication

Steven L Smith ssmith46 at zimbra.naz.edu
Thu Aug 7 16:55:58 CEST 2008


Hello, everyone-

I'm trying to both learn Python and develop a fairly robust web form with it at the same time... nothing like being thrown into a new job and having to learn a new language to do it!

We have ActiveState Python 2.5 installed on an IIS box running Windows 2003. We're doing it this way because we will most likely be switching to Apache / Unix in the future, and we don't want to have to rewrite all of our server-side scripts. Our web server is a domain member.

I am coding a form that will allow authenticated NT users to submit project requests. The form will, among other things, need to pull a list of domain users and put it into a dropdown menu, as well as show the username of the current user. Results will be dumped into an ODBC-connected mySQL database, which is working well (it's just the authentication issue that I'm struggling with).

I am able to connect to the domain with the following:

<%import ldap, sys, win32api
LDAP_SERVER='ldap://nazareth.internal'
LDAP_USERNAME='myusername at nazareth.internal'
LDAP_PASSWORD='mypassword'

try:
	ldap_client = ldap.initialize(LDAP_SERVER)
	ldap_client.simple_bind_s(LDAP_USERNAME, LDAP_PASSWORD)

except ldap.INVALID_CREDENTIALS, e:
	Response.Write("<h1 style='color:red'>Invalid credentials</h1>")
	sys.exit()
except ldap.SERVER_DOWN, e:
	Response.Write("<h1 style='color:red'>Your server appears to be down.</h1>")
	sys.exit()

Response.Write("<h1 style='color:blue'>Connected! All is well.</h1>")
ldap_client.unbind()
%>

However, other than hard-coding the variables, I don't know how to get them from the user, nor do I know how to actually authenticate them. Should I be using Cookies? How is that done?

As far as populating the form's <select> dropdown box, I've been using something similar to:

<%
import pyodbc
cnxn_ro = pyodbc.connect("DSN=metaproject_ro")
cursor = cnxn_ro.cursor()
cursor.execute("select * from Person order by last_name, first_name")
for row in cursor:
	Response.Write("<option value='"+row.uid+"'>"+row.last_name+", "+row.first_name+" ("+row.uid+")</option>")
cursor.close()
%>

(for testing purposes, I actually built a table in the database just so that I could populate it programmatically, but this is where I'd like to pull the info from Active Directory.



Hope someone can help! I've been scouring the web for several hours, and I'm actually *more* confused than when I started.


Thanks!


Steven L Smith
Web Developer
Nazareth College of Rochester


More information about the Tutor mailing list