Retrieve E-mail address from AD (Full Version)

All Forums >> [Web Development] >> ASP and Database



Message


zipler -> Retrieve E-mail address from AD (5/12/2006 14:01:23)

I have a website that is using the following code to retrieve a username from AD and then it uses the username to "guess" the persons e-mail address. This works most of the time, but we do have an increasing number of people whos e-mail addresses no longer match their login.

****Here is the code from the main ASP which uses an include to retrieve 2 variables from another ASP.

</td><td width=50%>
<!-- #include virtual="/forms/tools/UserNameTEST.asp"-->
<input type="hidden" name="SubmitName" value="<%= strdisplayname %>">
<input type="hidden" name="SubmitEmail" value="<%= strUserName %>@test.com">

<%= strdisplayname %> (<%= strUserName %>@test.com)
</font>


****Here is the Second ASP which does the work.

<%
'Function GetNames
' strUsername is the network login
' strdisplayname is the name (last, first)


'Option Explicit
Dim strUsername,strdisplayname,con,rsu,Com,objADsPath,objDomain

strUsername = LCase(Request.ServerVariables("auth_user"))
strUserName = Right(strUserName, Len(strUserName) - InStrRev(strUserName, "\"))
Set objDomain = GetObject ("GC://rootDSE")
objADsPath = objDomain.Get("defaultNamingContext")
Set objDomain = Nothing
Set con = Server.CreateObject("ADODB.Connection")
con.provider ="ADsDSOObject"
con.Properties("User ID") = "OurDomain\QueryLogin"
con.Properties("Password") = "password"
con.open "Active Directory Provider"
Set Com = CreateObject("ADODB.Command")
Set Com.ActiveConnection = con
Com.CommandText ="select displayname FROM 'GC://"+objADsPath+"' where sAMAccountname='"+strUsername+"'"
Set rsu = Com.Execute
strdisplayname = rsu("displayname")
rsu.Close
con.Close
Set rsu = Nothing
Set con = Nothing

'End Function
%>



Basically, what I would like to do is get it to pull the e-mail address stored in active directory rather than using the user name.
I believe I can change the following line and have what is returned assigned to strUserName but I do not know what I should select from objADsPath to return the e-mail.

Com.CommandText ="select displayname FROM 'GC://"+objADsPath+"' where sAMAccountname='"+strUsername+"'"


If anyone could help me out that would be great!..
Thank you!

Also, if it helps at all we are running Exchange 2003.




zipler -> RE: Retrieve E-mail address from AD (5/12/2006 19:35:50)

The fix was even easier than I thought. I opened up the Active Directory Schema editor to find the name of the variable I needed to pull

changed the following

Com.CommandText ="select displayname FROM 'GC://"+objADsPath+"' where sAMAccountname='"+strUsername+"'"
Set rsu = Com.Execute
strdisplayname = rsu("displayname")


to

Com.CommandText ="select mail FROM 'GC://"+objADsPath+"' where sAMAccountname='"+strUsername+"'"
Set rsu = Com.Execute
strdisplayname = rsu("mail")

Of course, I eneded up also adding in a simple left function to remove the @test.com so strdisplayname would still function the way it should in all of the forms. (since there are several hundred using the same include statement)


Thanks!




rdouglass -> RE: Retrieve E-mail address from AD (5/12/2006 23:45:31)

Thanks much for posting your solution. It does help others a lot. [:)]




JohnH -> RE: Retrieve E-mail address from AD (1/31/2008 12:40:44)

This is a great bit of code. Does anyone know how it can be adapted so that I can use it to populate a drop down box with all my users on my domain?

Many thanks,

John




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.03125