Jon Doe said:
Hi:
I'm a vb2005 newbie and need some direction. I want to create a form
that allows me to input a user id into a textbox, and then click a command
button to have it return the information about the user. The information
I'd like to retrieve is user id, ou, when password was changed and when it
is going to expire, telephone, address, etc...
I've been searching the internet for guidance but come up short trying
to piece everything together. Anybody know how to code this in VB2005?
Any help would be greatly appreciated and welcomed.
Thanks everyone,
My initial reaction was to say "have a look at what Microsoft already
provides", however, having in mind the Enterprise Library, all the core code
is C# (that give some pause for thought......)
Following is a class that may help in what you want. I cannot take credit
for this, one of our programmers, Paul wrote this, and it works really well.
I am sure the real experts can improve on this(no cynasism intended):
Imports System.DirectoryServices
Imports System.Data
Public Class AD_Helper
Implements IDisposable
Private DomainNameValue As String
Private ServerNameValue As String
Private GroupNameValue As String
#Region " Methods "
Public Function ReturnUsers(ByVal GroupName As String) As DataTable
If GroupName = String.Empty Then
Return Nothing
End If
Dim strDirEntryPath As String = "WinNT://" & DomainNameValue & "/" & _
ServerNameValue & "/" & GroupName & ",group"
Dim users As Object
Dim group As New DirectoryEntry(strDirEntryPath)
users = group.Invoke("members")
Dim ActiveDirTable As DataTable
ActiveDirTable = New DataTable("UserList")
Dim _UserName As DataColumn = New DataColumn("UserName")
Dim _DisplayName As DataColumn = New DataColumn("DisplayName")
_UserName.DataType = System.Type.GetType("System.String")
_DisplayName.DataType = System.Type.GetType("System.String")
ActiveDirTable.Columns.Add(_UserName)
ActiveDirTable.Columns.Add(_DisplayName)
Dim _PKs As DataColumn() = {_UserName}
ActiveDirTable.PrimaryKey = _PKs
For Each user1 As Object In CType(users, IEnumerable)
Dim userEntry As New DirectoryEntry(user1)
Dim fullName As String = GetUsersName(userEntry.Name)
Dim myNewRow As DataRow
myNewRow = ActiveDirTable.NewRow()
myNewRow("UserName") = userEntry.Name
myNewRow("DisplayName") = fullName
ActiveDirTable.Rows.Add(myNewRow)
ActiveDirTable.Select("", "DisplayName")
Next
Return ActiveDirTable
End Function
Public Function List_Users(ByVal GroupName As String) As BindingListView(Of
dtoUser)
Dim oList As New BindingListView(Of dtoUser)
If (GroupName = String.Empty) Then
Return oList
End If
Dim strDirEntryPath As String = "WinNT://" & DomainNameValue & "/" & _
ServerNameValue & "/" & GroupName & ",group"
Dim users As Object
Dim group As New DirectoryEntry(strDirEntryPath)
users = group.Invoke("members")
For Each oUser As Object In CType(users, IEnumerable)
Dim userEntry As New DirectoryEntry(oUser)
Using item As New dtoUser
With item
..Login = userEntry.Name
..DisplayName = GetUsersName(userEntry.Name)
End With
oList.Add(item)
End Using
Next
oList.SortByProperty("DisplayName", ListSortDirection.Ascending)
Return oList
End Function
Public Function GetUsersName(ByVal Username As String) As String
Dim strRealName As String = ""
Dim domain As String = DomainNameValue
Dim path As String = "LDAP://" + domain
Dim domainAndUsername As String = domain + "\" + Username
Dim entry As DirectoryEntry = New DirectoryEntry(path)
Dim Searcher As DirectorySearcher = New DirectorySearcher(entry)
Dim result As System.DirectoryServices.SearchResult
Try
If Username = String.Empty Then
Throw New Exception("The username is invalid")
Else
Searcher.Filter = ("(anr=" & Username & ")")
result = Searcher.FindOne()
If Not IsNothing(result) Then
strRealName = result.Properties("displayname")(0).ToString()
End If
End If
Return strRealName
Finally
Searcher.Dispose()
End Try
End Function
Public Function GetUserInfo(ByVal Username As String) As DataTable
Dim UserInfoTable As DataTable = New DataTable("UserInfo")
Dim _UserID As DataColumn = New DataColumn("UserName")
Dim _UsersName As DataColumn = New DataColumn("DisplayName")
Dim _UserEmail As DataColumn = New DataColumn("Email")
_UserID.DataType = System.Type.GetType("System.String")
_UsersName.DataType = System.Type.GetType("System.String")
_UserEmail.DataType = System.Type.GetType("System.String")
UserInfoTable.Columns.Add(_UserID)
UserInfoTable.Columns.Add(_UsersName)
UserInfoTable.Columns.Add(_UserEmail)
Dim domain As String = DomainNameValue
Dim path As String = "LDAP://" + domain
Dim domainAndUsername As String = domain + "\" + Username
Dim entry As DirectoryEntry = New DirectoryEntry(path)
Dim Searcher As DirectorySearcher = New DirectorySearcher(entry)
Dim result As System.DirectoryServices.SearchResult
Try
If Username = String.Empty Then
Throw New Exception("The username is invalid")
Else
Searcher.Filter = ("(anr=" & Username & ")")
result = Searcher.FindOne()
If Not IsNothing(result) Then
Dim myNewRow As DataRow
myNewRow = UserInfoTable.NewRow()
myNewRow("UserName") = Username
myNewRow("DisplayName") = result.Properties("displayname")(0).ToString()
Try
myNewRow("Email") = result.Properties("mail")(0).ToString()
Catch : End Try
UserInfoTable.Rows.Add(myNewRow)
End If
End If
Return UserInfoTable
Finally
Searcher.Dispose()
End Try
End Function
#End Region
#Region " Constructor "
Public Sub New(ByVal DomainName As String, ByVal ServerName As String)
DomainNameValue = DomainName
ServerNameValue = ServerName
End Sub
#End Region
#Region " IDisposable Support "
Private disposedValue As Boolean = False ' To detect redundant calls
' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' free managed resources when explicitly called
End If
' free shared unmanaged resources
End If
Me.disposedValue = True
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(ByVal disposing As
Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
End Class
Public Class dtoUser
Implements IDisposable
Private _Login As String
Private _DisplayName As String
#Region " Properties "
Public Property Login() As String
Get
Return _Login
End Get
Set(ByVal value As String)
If _Login = value Then
Return
End If
_Login = value
End Set
End Property
Public Property DisplayName() As String
Get
Return _DisplayName
End Get
Set(ByVal value As String)
If _DisplayName = value Then
Return
End If
_DisplayName = value
End Set
End Property
#End Region
#Region " IDisposable Support "
Private disposedValue As Boolean = False ' To detect redundant calls
' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' free managed resources when explicitly called
End If
' free shared unmanaged resources
End If
Me.disposedValue = True
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(ByVal disposing As
Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
End Class