Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2247
  • Last Modified:

VB.Net Enable Active Directory User Account

I need to be able to enable an Active Directory user account when I click submit. I have page setup with the following:
1. User select OU member from dropDownList1.
2. ObjUser=dropDownList1.SelectedValue
3. ObjUser is plugged into the Active Direct Search routine as so:
searchMe.Filter = "(&(objectclass=user)(samaccountname=" & objUser & "))"
4. This is used to populate other tables on the page such as the account description etc.

Below is the code for adsearcher. Is there another attribute I need to be pulling during this sub and what would I need to set it to in order to enable the user account?

Thanks in advance!!
Public Sub adSearcher()
        Dim adInfo As New DirectoryServices.DirectoryEntry(strLpath)
        Dim searchMe As New System.DirectoryServices.DirectorySearcher(adInfo)
 
        searchMe.PropertiesToLoad.Add("displayName")
        searchMe.PropertiesToLoad.Add("Description")
        searchMe.Sort.Direction = SortDirection.Ascending
        searchMe.PageSize = 10
        searchMe.Filter = "(&(objectclass=user)(samaccountname=" & objUser & "))"
 
        searchMe.SearchScope = SearchScope.Subtree
        Dim results As SearchResultCollection
        results = searchMe.FindAll()
        Dim result As SearchResult
 
        arrCount = intcount - 1
 
        Dim myDarr1(arrCount, 7) As String
        arrCount = 0
 
        For Each result In results
            intcount = intcount + 1
        Next
        If InStr(result.Properties("DisplayName")(0), "$") Then
 
        Else
 
            Try
                resDescription = result.Properties("description")(0)
            Catch ex As Exception
                resDescription = ""
            End Try
 
            TextBox1.Text = resdescription
 
        End If
       
    End Sub

Open in new window

0
JB4375
Asked:
JB4375
  • 4
  • 4
1 Solution
 
CodeCruiserCommented:
Check the heading number 7 in the following link

http://www.vbdotnetheaven.com/UploadFile/ecabral/ADandNETInVB11112005020216AM/ADandNETInVB.aspx?ArticleID=d4c079fb-0f31-40a4-8917-88e54839c1b5
7. Enable a user account.
 
''' <summary>
''' Method to enable a user account in the AD.
''' </summary>
''' <param name="de"></param>
Private Shared Sub EnableAccount(ByVal de As DirectoryEntry)
'UF_DONT_EXPIRE_PASSWD 0x10000
Dim exp As Integer = CInt(de.Properties("userAccountControl").Value)
de.Properties("userAccountControl").Value = exp Or &H0001
de.CommitChanges()
'UF_ACCOUNTDISABLE 0x0002
Dim val As Integer = CInt(de.Properties("userAccountControl").Value)
de.Properties("userAccountControl").Value = val And Not &H0002
de.CommitChanges()
End Sub

Open in new window

0
 
JB4375Author Commented:
I get what it's supposed to be doing, however I'm at a loss on how to pass the argument to the Enable_Account Sub. I have a dropdownlist that lists the member of the group, the person selects from that sorted list and then clicks a button to enable the account.
Below is the my code for the drop down, the call, etc.
Thanks
 

Public Sub FillDropdown()
        Dim ldapPath As String = "LDAP://DC=domain,DC=com"
        Dim de As New DirectoryEntry(ldapPath)
        Dim ds As New DirectorySearcher(de)
        ds.PageSize = 1000
        ds.Filter = "(objectCategory=User)"
        Dim colUsers As ArrayList
        colUsers = New ArrayList
        Dim result As SearchResult
 
        For Each result In ds.FindAll()
            colUsers.Add(result.Properties("cn")(0))
        Next result
        colUsers.Sort()
        DropDownList1.DataSource = colUsers
        DropDownList1.DataBind()
        de.Username = "domain\" & DropDownList1.SelectedItem.Text
    End Sub
 
Call Enable_Account(????) No idea....doesn't like de.UserName though
 
Private Shared Sub Enable_Account(ByVal de As DirectoryEntry)
        'UF_DONT_EXPIRE_PASSWD 0x10000
        Dim exp As Integer = CInt(de.Properties("userAccountControl").Value)
        de.Properties("userAccountControl").Value = exp Or &H1
        de.CommitChanges()
        'UF_ACCOUNTDISABLE 0x0002
        Dim val As Integer = CInt(de.Properties("userAccountControl").Value)
        de.Properties("userAccountControl").Value = val And Not &H2
        de.CommitChanges()
    End Sub

Open in new window

0
 
CodeCruiserCommented:
You need to pass the de to the sub. When button is clicked, set the de properly. For example
de.Path = "LDAP://192.168.1.1/CN=Users;DC=Yourdomain"

Where you would use the IP address of your AD controller and you domain.
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
JB4375Author Commented:
OK.
1. I query AD to fill the drop down with members of an OU.
2. I select and account from the list, and on postback the attribute "account description" of the selected item is displayed in another field.
3. I click submit, and the Enable_Account sub is called.
Since I've pointed to the OU it's located in, and displayed it attributes, I was under the impression that I already had the object itself.  Why am I needing to pass the path again instead of just being able to change one of the object's other attributes?
I feel like I'm missing what seems like a minor detail, but is actually totally necessary for what I'm trying to accomplish.
0
 
CodeCruiserCommented:
Hi,
Sorry for late response. I did not understand your point. You are only passing the DirectoryEntry object to the Enable_Account sub.
0
 
JB4375Author Commented:
Sorry to be slow on the uptake........
"You are only passing the DirectoryEntry object to the Enable_Account sub."
Is this what I SHOULD be doing, and I'm failing to do so, OR
Is this what I'm currently doing, when I should be doing something else?
0
 
CodeCruiserCommented:
Your code looks absolutly fine. The passing of DirectoryEntry is fine as well.
0
 
JB4375Author Commented:
Thanks for help and patience. ;-)
0

Featured Post

Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now