Active Directory and ADSI

[Previous] [Next]

To show how you can use your ADSI skills against the Exchange Server 5.5 directory and Active Directory, I've created a simple Web application that draws an organizational chart from both these directories. As you'll see in the code, you can easily query both directories. You'll also see that programming both directories using ADSI is similar. The only differences are the path you need to provide for LDAP binding and some of the property names. Obviously, more differences arise when you start performing more complex operations, such as setting securities and creating users.

Figure 15-13 shows the Logon page for the Web-based application. On this page, the user can select which directory he wants to connect to, thereby executing an ASP page that sets the correct credentials for the directory and then calls to the organizational chart ASP program. The JavaScript in the Web page displays or hides text boxes that ask for the alias or full user name.

NOTE
There is a difference between Exchange Server and Active Directory in the form of LDAP paths they expect. Exchange Server expects the alias of the user (for example, Thomriz), while Active Directory expects the name of the user (for example, Thomas Rizzo). Watch out for this in your applications.

click to view at full size.

Figure 15-13. The Logon page for the organizational chart ADSI sample.

Figure 15-14 shows how to pull organizational information from the Exchange Server directory. The Web page uses a Java applet that displays the organizational chart and provides hyperlinks so that you can click on the hyperlink for another user to see that user's organizational information.

click to view at full size.

Figure 15-14. The organizational information from an Exchange Server 5.5 directory.

The code to perform this functionality is straightforward. It first connects to the Exchange server—specifically the object that corresponds to the alias passed in. Then, the code retrieves the user information, such as office location, phone number, title, and alias name. Next, the code tries to retrieve the same information for the user's manager by querying the manager property. The code then attempts to open the manager's object from the directory and retrieve the same information from that object. Finally, the code performs the same functions for the specified user's direct reports and passes this information into the Java applet. I've left some of the debugging statements in the code; you can uncomment them and see exactly what's happening at your leisure.

 if Session("Type") = "EXCHANGE" then 'Get if from Exchange! Set oIADs = GetObject("LDAP:") bstr1 = "LDAP://" + Session("Server") + "/cn=" + strUser + ",cn=" + Session("cn") + ",ou=" + Session("OU") + ",o=" + Session("O") 'bstr2 = Session("bstr2") 'bstr3 = Session("bstr3") 'Response.Write bstr1 'Response.Write "bstr2=" & bstr2 'Response.Write "bstr3=" & Session("bstr3") 'Attempt an anonymous bind; if this doesn't work, you'll need 'to uncomment the line following the one below Set objIADs = oIADs.OpenDSObject(bstr1, "","",0) 'Set objIADs = oIADs.OpenDSObject(bstr1,bstr2,bstr3,0) 'Get the current person's information strOffice = objIADs.Get("physicalDeliveryOfficeName") strTelephone = objIADs.Get("telephoneNumber") strTitle = objIADs.Get("title") strcn = objIADs.Get("cn") strrdn = objIADs.Get("rdn") strManager = objIADs.Get("manager") 'Response.Write strManager strManagerPath = "LDAP://" & Session("Server") & "/" & strManager set oIADsManager = oIADS.OpenDSObject(strManagerPath,"","",0) 'Anonymous bind; uncomment the line below if it doesn't work set oIADsManager = oIADS.OpenDSObject(strManagerPath,bstr2,bstr3,0) strManagercn = oIADsManager.Get("cn") strManagerrdn = oIADsManager.Get("rdn") 'Response.Write "mcn=" & strManagercn 'Response.Write "crdn=" & strManagerrdn strManagerOffice = oIADsManager.Get("physicalDeliveryOfficeName") strManagerTelephone = oIADsManager.Get("telephoneNumber") strManagerTitle = oIADsManager.Get("title") strReports = objIADs.GetEx("Reports") 'Response.Write "Upper: " & UBound(strReports) 'Response.Write "Lower: " & lbound(strReports) %> <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 3.2//EN"> <HTML> <HEAD> <Title>Web Org-Chart (java version)</Title> </HEAD> <center><font face=Verdana size=6><B>From the Exchange Directory</B></center></font> <p align="CENTER"> <applet codebase="java" code=JOrgChart.class id=JOrgChart width=480 <% if UBound(strReports) = LBound(strReports) then iHeight = 0 else iHeight = UBound(strReports) end if %> height=<%=(Int(((iHeight)/2)+3)*65)%>> <param name=HostName value="<% =ROOTURL %>?Alias="> <param name=Root value="<%=strManagerrdn%>? <%=strManagercn%>? <%=EmptyToNA(strManagerTitle)%>? <%=EmptyToNA(strManagerOffice)%>? <%=EmptyToNA(strManagerTelephone)%>"> <param name=L1Node value="<%=strrdn%>?<%=strcn%>?<%=strTitle%>?<%=strOffice%>?<%=strTelephon e%>"> <% on error resume next for i = LBound(strReports) to UBound(strReports) strLogonName = _ left(strReports(i),(instr(1,strReports(i),",")-1)) 'Get each DS object to return the friendly name strDirectPath = "LDAP://" & Session("Server") & _ "/" & strReports(i) set oIADsReports = oIADs.OpenDSObject(strDirectPath, "","",0) 'Anonymous bind; uncomment if it doesn't work set oIADsReports = _ oIADs.OpenDSObject(strDirectPath, bstr2,bstr3,0) strReportscn = oIADsReports.Get("cn") 'Response.Write "cn" & strReportscn strReportsOffice = oIADsReports.Get("physicalDeliveryOfficeName") strReportsTelephone = _ oIADsReports.Get("telephoneNumber") strReportsTitle = oIADsReports.Get("title") strReportsrdn = oIADsReports.Get("rdn") %> <param name=L2Node<%=i%> value="<%=strReportsrdn%>? <%=strReportscn%>? <%=EmptyToNA(strReportsTitle)%>? <%=EmptyToNA(strReportsOffice)%>? <%=EmptyToNA(strReportsTelephone)%>"> <% Next %> </applet> </p> <P> <B>Select a different alias by clicking <a href="logon.asp">here.</B> </BODY> 

Figure 15-15 shows how to query similar information from Active Directory using LDAP, and the code for this query follows. Notice in the code that the LDAP path is a bit different from the one specified in the earlier code. Instead of specifying O (organization) and OU (organizational unit), you specify DC (domain controllers). If you want to simply grab information from Active Directory, you'll find your Exchange Server directory ADSI skills quite useful.

click to view at full size.

Figure 15-15. Querying information from Active Directory.

 <% else %> <% 'Get if from Active Directory Set oIADs = GetObject("LDAP:") bstr1 = "LDAP://" + Session("Server") + "/cn=" + strUser + ",cn=" + Session("cn") + ",dc=" + Session("Domain") + ",dc=" + Session("DC") + ",dc=" + Session("OU") + ",dc=" + Session("o") 'bstr1 = "LDAP://" + Session("Server") + "/" + "cn=thomriz,cn=Users,dc=ADCDEMO,DC=extest,DC=Microsoft,DC=com" 'bstr1 = "LDAP://" + Session("Server") + "/" + Request.Form("cn") + ",ou=" + Session("OU") + ",o=" + Session("O") bstr2 = Session("bstr2") bstr3 = Session("bstr3") 'Response.Write bstr1 'Response.Write "bstr2=" & bstr2 'Response.Write "bstr3=" & Session("bstr3") Set objIADs = oIADs.OpenDSObject(bstr1, bstr2, bstr3, 0) 'Response.Write objIADs.Get("manager") 'Get the current person's information strDisplayName = objIADs.Get("displayName") strOffice = objIADs.Get("physicalDeliveryOfficeName") strTelephone = objIADs.Get("telephoneNumber") strTitle = objIADs.Get("title") strcn = objIADs.Get("cn") 'Try to retrieve the Mail property strmail = "" strmail = objIADs.Get("mail") if strmail = "" then 'Try getting mailNickname strmail = objIADs.Get("mailNickname") end if 'Response.Write "strcn= " & strcn strManager = objIADs.Get("manager") 'Response.Write strManager strManagerPath = "LDAP://" & Session("Server") & "/" & strManager 'Response.Write strManagerPath set oIADsManager = oIADS.OpenDSObject(strManagerPath,bstr2,bstr3,0) strManagercn = oIADsManager.Get("cn") 'Try to retrieve the Mail property strManagerMail = "" strManagerMail = oIADsManager.Get("mail") if strManagerMail = "" then strManagerMail = oIADsManager.Get("mailNickname") end if 'Response.Write "mcn=" & strManagercn strManagerOffice = oIADsManager.Get("physicalDeliveryOfficeName") strManagerTelephone = oIADsManager.Get("telephoneNumber") strManagerTitle = oIADsManager.Get("title") strManagerDisplayName = oIADsManager.Get("displayName") strReports = objIADs.GetEx("directReports") %> <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 3.2//EN"> <HTML> <HEAD> <Title>Web Org-Chart (java version)</Title> </HEAD> <center><font face=Verdana size=6><B>From Active Directory</B></center></font> <p align="CENTER"> <applet codebase="java" code=JOrgChart.class id=JOrgChart width=480 <% if UBound(strReports) = LBound(strReports) then iHeight = 0 else iHeight = UBound(strReports) end if %> height=<%=(Int(iHeight/2)+3)*65%>> <param name=HostName value="<% =ROOTURL %>?Alias="> <param name=Root value="<%=strManagerDisplayName%>? <%=strManagerMail%>? <%=EmptyToNA(strManagerTitle)%>? <%=EmptyToNA(strManagerOffice)%>? <%=EmptyToNA(strManagerTelephone)%>"> <param name=L1Node value="<%=strDisplayName%>?<%=strMail%>?<%=EmptyToNA(strTitle)%>?<%=Empty ToNA(strOffice)%>?<%=EmptyToNA(strTelephone)%>"> <% for i = LBound(strReports) to UBound(strReports) strLogonName = left(strReports(i),(instr(1,strReports(i),",")-1)) 'Get each DS object to return the friendly name strDirectPath = "LDAP://" & Session("Server") & "/" & strReports(i) set oIADsReports = oIADs.OpenDSObject(strDirectPath, bstr2,bstr3,0) strReportscn = oIADsReports.Get("cn") 'Try to get the mail address strReportsMail = "" strReportsMail = oIADsReports.Get("mail") if strReportsMail = "" then strReportsMail = oIADsReports.Get("mailNickname") end if strReportsOffice = oIADsReports.Get("physicalDeliveryOfficeName") strReportsTelephone = oIADsReports.Get("telephoneNumber") strReportsTitle = oIADsReports.Get("title") strReportsDisplayName = oIADsReports.Get("displayName") %> <param name=L2Node<%=i%> value="<%=strReportsDisplayName%>? <%=strReportsMail%>? <%=EmptyToNA(strReportsTitle)%>? <%=EmptyToNA(strReportsOffice)%>? <%=EmptyToNA(strReportsTelephone)%>"> <% Next %> </applet> </p> <P> <B>Select a different alias by clicking <a href="logon.asp">here.</B> </BODY> 



Programming Microsoft Outlook and Microsoft Exchange
Programming Microsoft Outlook and Microsoft Exchange, Second Edition (DV-MPS Programming)
ISBN: 0735610193
EAN: 2147483647
Year: 2000
Pages: 184

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net