.NET Programmer Thoughts

Wednesday, January 26, 2005

Retrieve Users From a Active Directory Security Group

Last week I was asked to add some security to the application I had written. It is basically a front-end to several access databases. It displays all the available access applications and the user can double click on one and open it. The owner of the application said users should not be able to see the whole list. Only certain users need to see certain applications.

This meant that I was going to have to maintain a user list. I really didn't want to do this. So I googled around and found that I could access the users assigned to the security group assigned to my front-end application.

The call:
Dim myADGroup As AD_Group
Dim myUserList As DataTable
myADGroup = New _Group("TrishDomain", "TrishDC", "AppsSecGroup")
myUserList = myADGroup.ReturnUsers()
Unfortunately all this work was done in VB.NET. I'm still too new at all this to do the conversion to C# for you. I also had to do some reformatting to get it into this blog so if it doesn't work just let me know and I will email you the source file.

ReturnUsers() will return a DataTable of all the users in the Active Directory Security group. The DataTable will have the user's ID and full name. DomainName is the name of the domain. ServierName is the name of a domain controller. GroupName is the name of the Security group.

Imports System.DirectoryServices

Public Class AD_Group
Private DomainNameValue As String
Private ServerNameValue As String
Private GroupNameValue As String
Public Sub New(ByVal DomainName As String, ByVal ServerName As String, ByVal GroupName As String)
DomainNameValue = DomainName
ServerNameValue = ServerName
GroupNameValue = GroupName
End Sub

Public Function ReturnUsers()

Dim strDirEntryPath As String
strDirEntryPath = "WinNT://" + DomainNameValue
+ "/" + ServerNameValue + "/" + GroupNameValue + ",group"
Dim users As Object
Dim group As New DirectoryEntry(strDirEntryPath)

users = group.Invoke("members")

Dim user1 As Object

Dim ActiveDirTable As DataTable
ActiveDirTable = New DataTable("UserList")
Dim UserID As DataColumn = New DataColumn("UserID")
Dim UserName As DataColumn = New DataColumn("UserName")
UserID.DataType = System.Type.GetType("System.String")
UserName.DataType = System.Type.GetType("System.String")

For Each user1 In CType(users, IEnumerable)

Dim userEntry As New System.DirectoryServices.DirectoryEntry(user1)
Dim fullName As String = GetUserInfo(userEntry.Name, userEntry.Password)
Dim myNewRow As DataRow
myNewRow = ActiveDirTable.NewRow()
myNewRow("UserID") = userEntry.Name
myNewRow("UserName") = fullName

Return ActiveDirTable
End Function

Public Function GetUserInfo(ByVal username As String, ByVal pwd As String) As String
Dim strRealName As String = ""
If username = "" Or username = Nothing Then
strRealName = "Invalid Signature"
pwd = Nothing ' works better if pwd is nothing
Dim domain As String = DomainNameValue
Dim path As String = "LDAP://" + domain
Dim domainAndUsername As String = domain + "\" + username
Dim entry As DirectoryEntry = New DirectoryEntry(path, domainAndUsername, pwd)
Dim Searcher As DirectorySearcher = New DirectorySearcher(entry)
Dim result As System.DirectoryServices.SearchResult
Searcher.Filter = ("(anr=" & username & ")")
result = Searcher.FindOne()
If Not IsNothing(result) Then
strRealName = result.Properties("givenName")(0).ToString() & " " & result.Properties("sn")(0).ToString()
End If
End If
Return strRealName

End Function

End Class

Sunday, January 09, 2005

Catch Up Time

It's been a while since I've blogged. I have been on assignment at "Company A". They acquired a company that had several Access databases. These databases were written by a barge Captain. So you can just image how the code looks! Everytime they tried to move the applications to another server everything would crash. So my job was to get it running in a clean environment. The other catch was that it was working against Novell's Groupwise and they want it running on Exchange. So I've been pretty busy.

There was lots of hard-coded paths and email addresses. This was the major problem. So I had to learn some VBA (Visual Basic For Applications). Then I wrote a front-end to all the databases in VB.NET. Yes, I know. I'm a sharpie. But they told me they wanted the code in VB.NET.

I dont' care what anyone says. C# and VB are not the same. The first time I got hit with "Nothing", it took me about two hours to figure it out. There doesn't seem to be "Null" in VB. It's "Nothing". The other big thing is that the properties windows doesn't show you the list of events for the control. Which means you can't double click on the event and have it write the code structure for you. Which also means that you have to remember what events are available and what their signatures are.

So I can say now that I've written my first .NET application. I'm told I will have a code review next week. SARK sends me out to a site all by myself, with no .NET experience, and then wants to do a code review. Well, it's good code. Bring it on!