SWRO
asked on
Convert LDAP query from ASP to ASP.NET
I use the following code on an ASP page and it works fine. However, I need to upgrade the page to ASP.NET (VB.NET) and this doesn't work. I get the Set and Let no longer supported error message.
What's the ASP.NET equivalent of this?
<%
strUser= Replace(Request.ServerVari ables("LOG ON_USER"), "\", "/")
On Error Resume Next
Set xUser = GetObject("WinNT://" & strUser & ",user")
usrUserName = xUser.Name
usrFullname = xUser.Fullname
nameArray = Split(usrFullname, " ", -1, 1)
usrFirstName = nameArray(LBound(nameArray ))
usrLastName = nameArray(UBound(nameArray ))
%>
What's the ASP.NET equivalent of this?
<%
strUser= Replace(Request.ServerVari
On Error Resume Next
Set xUser = GetObject("WinNT://" & strUser & ",user")
usrUserName = xUser.Name
usrFullname = xUser.Fullname
nameArray = Split(usrFullname, " ", -1, 1)
usrFirstName = nameArray(LBound(nameArray
usrLastName = nameArray(UBound(nameArray
%>
don't forget to add "system.directoryservice.d ll" as references
get all user information
Public Function GetallUserInfo()
Try
Dim enTry As DirectoryEntry = New DirectoryEntry("LDAP://CN= users,DC=h r,DC=com")
Dim mySearcher As DirectorySearcher = New DirectorySearcher(enTry)
Dim myResultPropColl As ResultPropertyCollection
Dim myResultPropValueColl As ResultPropertyValueCollect ion
Dim mySearchResultColl As SearchResultCollection
Dim i As Integer
Dim mySearchResult As SearchResult
mySearcher.Filter = ("(objectClass=user)")
mySearchResultColl = mySearcher.FindAll()
Select Case mySearchResultColl.Count
Case 0
Return "Null"
Exit Function
End Select
For i = 0 To mySearchResultColl.Count - 1
mySearchResult = mySearchResultColl.Item(i)
myResultPropColl = mySearchResult.Properties
myResultPropValueColl = myResultPropColl.Item("Des cription")
Response.Write(myResultPro pValueColl .Item(0))
Response.Write("<br>")
Next
Catch ex As System.Exception
Label6.Text = ex.Message
End Try
End Function
Public Function GetallUserInfo()
Try
Dim enTry As DirectoryEntry = New DirectoryEntry("LDAP://CN=
Dim mySearcher As DirectorySearcher = New DirectorySearcher(enTry)
Dim myResultPropColl As ResultPropertyCollection
Dim myResultPropValueColl As ResultPropertyValueCollect
Dim mySearchResultColl As SearchResultCollection
Dim i As Integer
Dim mySearchResult As SearchResult
mySearcher.Filter = ("(objectClass=user)")
mySearchResultColl = mySearcher.FindAll()
Select Case mySearchResultColl.Count
Case 0
Return "Null"
Exit Function
End Select
For i = 0 To mySearchResultColl.Count - 1
mySearchResult = mySearchResultColl.Item(i)
myResultPropColl = mySearchResult.Properties
myResultPropValueColl = myResultPropColl.Item("Des
Response.Write(myResultPro
Response.Write("<br>")
Next
Catch ex As System.Exception
Label6.Text = ex.Message
End Try
End Function
ASKER
Wow, 9 lines of code turned into 50. I'll give it a shot.
Though, isn't there something with the page directive and removing the SET statement that might also work?
Though, isn't there something with the page directive and removing the SET statement that might also work?
no no, first function is for add new user to active directory by asp.net, first time I sent that function in mistake and because I couldn't delete my comments I just added new comment
in asp.net we don't have "set line" instead you need to declare like this
Dim enTry As DirectoryEntry = New DirectoryEntry("LDAP://CN= users,DC=h r,DC=com")
how ever instead of LDAP you could use WINNT, but then just you could use that code for windows NT and 2000
Dim enTry As DirectoryEntry = New DirectoryEntry("LDAP://CN=
how ever instead of LDAP you could use WINNT, but then just you could use that code for windows NT and 2000
instead of DC = your domain forinstance yahoo.com will be DC=yahoo, DC=com
CN = common name for instance if your user is in Organization unit (OU=) or just in Users and ... you could use it
about windows NT , the code will be alittle different, if you need it, I will send you that too.
CN = common name for instance if your user is in Organization unit (OU=) or just in Users and ... you could use it
about windows NT , the code will be alittle different, if you need it, I will send you that too.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. I will give that a shot.
' Dim ADsRoot = GetObject("GC://rootDSE")
' Dim strRootForest As String = "LDAP://" & ADsRoot.get("rootDomainNam
'Connect to computers in a domain. For example, "LDAP://CN=<computer name>, CN=<Computers>, DC=<domain controller1>, DC=<domain controller2>,...".
Dim Ldap As DirectoryEntry = New DirectoryEntry("LDAP://cn=
Dim group As DirectoryEntry = Ldap.Children.Find("cn=Stu
Dim group1 As DirectoryEntry = Ldap.Children.Find("cn=msc
Dim user As DirectoryEntry = Ldap.Children.Add("cn=" & strLogin, "User")
'userlogin name
user.Properties("SAMAccoun
' lastname
With user
'.Properties("sn").Add("la
'.Properties("givenName").
.Properties("DisplayName")
.Properties("Description")
' .Properties("groupType").V
'Password never expired
.Properties("userAccountCo
'account not disabled
'.Properties("userAccountC
.CommitChanges()
End With
'rename user
'user.Rename("cn=sara")
user.Invoke("SetPassword",
user.CommitChanges()
'to add user to new group
Dim strDisName As String = user.Properties("distingui
group.Invoke("Add", New Object() {"LDAP://" & strDisName})
group.CommitChanges()
group1.Invoke("Add", New Object() {"LDAP://" & strDisName})
group1.CommitChanges()
End Sub