Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_professional thread: A System.DirectoryServices Chanllenge


Message #1 by e.soonius@m... on Fri, 19 Jul 2002 17:37:07
I have a strange problem with a LDAP query against an Active Directory. 
If I access the properties of the search result I get the following 
error: Handling of this ADSVALUE type is not yet implemented type = 0xb 
on a Windows 2000 Adv Server.

The same code executed on a Windows XP Pro runs without any errors...

Hereunder I have added the code that I use to test.

In IIS is the Anonymous access is disabled;
Integrated Windows authentication only

Added to the Web.Config: <identity impersonate="true" />

I could use some advise.
Thanks in advance,

Eric

<VBCode>
Imports System
Imports System.Diagnostics
Imports System.DirectoryServices
Imports System.Security.Principal

Public Class WebForm1
	Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

		'This call is required by the Web Form Designer.
		<System.Diagnostics.DebuggerStepThrough()> _
		Private Sub InitializeComponent()

		End Sub

		Private Sub Page_Init(ByVal sender As System.Object, 
ByVal e As System.EventArgs) Handles MyBase.Init
				'CODEGEN: This method call is required by 
the Web Form Designer
				'Do not modify it using the code editor.
				InitializeComponent()
		End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles MyBase.Load
	'Put user code to initialize the page here
	If User.Identity.IsAuthenticated Then

		'create a reference to a WindowsIdentity object that 
represents this users Indentity object
		Dim objIdentity As WindowsIdentity = CType(User.Identity, 
WindowsIdentity)

		'get the current WindowsIdentity object for this user 
this contains more specific information
		Dim objWinIdentity As WindowsIdentity = 
objIdentity.GetCurrent()

		'get the name (Domain\SAMaccountname) of the 
authenticated user and split it into domain and name
		Response.Write("NTDomain = " & Left(objWinIdentity.Name, 
InStr(objWinIdentity.Name, "\") - 1) & "<BR/>")
		Response.Write("SAMaccount = " & Right
(objWinIdentity.Name, Len(objWinIdentity.Name) - InStr
(objWinIdentity.Name, "\")) & "<BR/>")

		GetUserInfo(Right(objWinIdentity.Name, Len
(objWinIdentity.Name) - InStr(objWinIdentity.Name, "\")))

	Else

		Response.Write("You are not authenticated.")

	End If

	End Sub

	Private Sub GetUserInfo(ByVal SAMAccount As String)
		Dim LDAPServer As String = "LDAP://mydomain.com"

		Dim myDirectory As New DirectoryEntry(LDAPServer)
		Dim mySearcher As New DirectorySearcher(myDirectory)
		Dim mySearchResultColl As SearchResultCollection
		Dim mySearchResult As SearchResult
		Dim myResultPropColl As ResultPropertyCollection
		Dim myResultPropValueColl As ResultPropertyValueCollection

		'Build LDAP query
		mySearcher.Filter = ("(&(objectClass=user)
(samaccountname=" & SAMAccount & "))")
		mySearchResultColl = mySearcher.FindAll()

		'I expect only one user from search result
		Select Case mySearchResultColl.Count
			Case 0
				Response.Write("The SAM Account Name 
cannot be found in the Directory.")
				Exit Sub
			Case Is > 1
				Response.Write("The SAM Account Name is 
ambiguous.")
				Exit Sub
		End Select

		'Get the search result from the collection
		mySearchResult = mySearchResultColl.Item(0)

		'Get the Properites, they contain the usefull info
		myResultPropColl = mySearchResult.Properties

		'Retrieve from the properties collection the display name 
and email of the user
		myResultPropValueColl = myResultPropColl.Item
("displayname")
		Response.Write("LDAP DisplayName = " & CStr
(myResultPropValueColl.Item(0)) & "<BR/>")

		myResultPropValueColl = myResultPropColl.Item("mail")
		Response.Write("LDAP EMail = " & CStr
(myResultPropValueColl.Item(0)) & "<BR/>")
	End Sub

End Class
</VBCode>

  Return to Index