Link to home
Start Free TrialLog in
Avatar of SWRO
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.ServerVariables("LOGON_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))
%>
Avatar of sara110
sara110

Public Sub AddUser(ByVal strLogin As String, ByVal strPwd As String, ByVal strname As String, ByVal strdescription As String)
        ' Dim ADsRoot = GetObject("GC://rootDSE")
        '  Dim strRootForest As String = "LDAP://" & ADsRoot.get("rootDomainNamingContext")
        '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= Users,dc=hr,dc=aut,dc=ac,dc=ir", "administrator or user which could make account", "password", AuthenticationTypes.Sealing Or AuthenticationTypes.Secure)
        Dim group As DirectoryEntry = Ldap.Children.Find("cn=Students", "Group")
        Dim group1 As DirectoryEntry = Ldap.Children.Find("cn=msc", "Group")

        Dim user As DirectoryEntry = Ldap.Children.Add("cn=" & strLogin, "User")
        'userlogin name
        user.Properties("SAMAccountName").Add(strLogin)
        '  lastname

        With user

            '.Properties("sn").Add("lastname")
            '.Properties("givenName").Add(strname)
            .Properties("DisplayName").Add(strname)
            .Properties("Description").Add(strdescription)
            ' .Properties("groupType").Value = _UNIVERSAL_SECURITY
            'Password never expired
            .Properties("userAccountControl").Value = 65536
            'account not disabled
            '.Properties("userAccountControl").Value = 512

            .CommitChanges()

        End With
        'rename user
        'user.Rename("cn=sara")

        user.Invoke("SetPassword", New Object() {strPwd})
        user.CommitChanges()
        'to add user to new group
        Dim strDisName As String = user.Properties("distinguishedName").Value
        group.Invoke("Add", New Object() {"LDAP://" & strDisName})
        group.CommitChanges()
        group1.Invoke("Add", New Object() {"LDAP://" & strDisName})
        group1.CommitChanges()
    End Sub
don't forget to add "system.directoryservice.dll" as references
get all user information
Public Function GetallUserInfo()
        Try
            Dim enTry As DirectoryEntry = New DirectoryEntry("LDAP://CN=users,DC=hr,DC=com")
            Dim mySearcher As DirectorySearcher = New DirectorySearcher(enTry)
            Dim myResultPropColl As ResultPropertyCollection
            Dim myResultPropValueColl As ResultPropertyValueCollection
            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("Description")
                Response.Write(myResultPropValueColl.Item(0))
                Response.Write("<br>")
            Next

        Catch ex As System.Exception
            Label6.Text = ex.Message
        End Try
    End Function
Avatar of SWRO

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?
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=hr,DC=com")
 
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.
ASKER CERTIFIED SOLUTION
Avatar of sara110
sara110

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SWRO

ASKER

Thanks. I will give that a shot.