Getting the user's DN

D

Derek Martin

Using VB.Net, I would like to retrieve the currently logged in user's DN
from Active Directory. Alternatively, if, using WindowsIdentity, or
something similar, I would like to get the user's full name that is found on
the Workstation Locked screen between the ( )'s.

Does anyone know how to do that? The only constraint: no use of
ActiveDS.dll permitted by the design.

Many thanks!
Derek
 
K

Ken Tucker [MVP]

Hi,

Systeminformation.username, systeminformation.userdomain,
environment.username, environment.userdomain

Ken
-------------------
Using VB.Net, I would like to retrieve the currently logged in user's DN
from Active Directory. Alternatively, if, using WindowsIdentity, or
something similar, I would like to get the user's full name that is found on
the Workstation Locked screen between the ( )'s.

Does anyone know how to do that? The only constraint: no use of
ActiveDS.dll permitted by the design.

Many thanks!
Derek
 
D

Derek Martin

Thank you for your reply Ken, however, none of these appear to get me where
I need. For instance, my DN is this:
CN=Derek M. Martin,OU=support,OU=users,OU=level,DC=dc,DC=domain,DC=com

When I lock the computer, it says: Only network\username (Derek M. Martin)
.... bla bla bla

What I need is Derek M Martin to come out.

Can you assist? Thanks again for the reply!

Derek
 
D

Derek Martin

Does no one know how to do this??? :-(


Derek Martin said:
Thank you for your reply Ken, however, none of these appear to get me
where I need. For instance, my DN is this:
CN=Derek M. Martin,OU=support,OU=users,OU=level,DC=dc,DC=domain,DC=com

When I lock the computer, it says: Only network\username (Derek M. Martin)
... bla bla bla

What I need is Derek M Martin to come out.

Can you assist? Thanks again for the reply!

Derek
 
S

Steve Long

Derek, this is more than just your domain name. Can you put a name on this
set of information that you have listed below?

Steve
 
D

Derek Martin

Hi Steve, this is the Distinguished Name, DN. What I ended up doing was
instead of looking for cn, I am looking at LDAP for a sAMAccountName, which
allowed me to do a search on the username instead of the DN.

So, I appear to be in good shape :)

Thanks a bunch!
 
S

Steve Long

would you mind sharing with me how you are doing that? I do find the problem
interesting.

Steve
 
D

Derek Martin

Hey Steve:

Basically, what I did was take about 10 examples from around the web, throw
in my own little bit and bingo it worked. Here is the code, some comments
along the way:

'Start up code:
Dim wi As WindowsIdentity = WindowsIdentity.GetCurrent
Dim logonname As String = wi.Name.ToString
Dim a As Boolean = security.checkpermissions("AD-GROUP-NAME",
logonname.Substring(3)) 'This is what group I want and what user to check
on, here, the currently logged in user
RichTextBox1.Clear()
RichTextBox1.AppendText(a.ToString)

'Calls CHECKPERMISSIONS, which is just a public exposer for the private
function
Public Shared Function checkpermissions(ByVal groupname As String, ByVal
username As String)
Dim results As Boolean = IsMember(global.groupdomain, groupname,
username)
Return results
End Function

Which calls IsMember:
Private Shared Function IsMember(ByVal strDomain As String, ByVal strGroup
As String, ByVal strMember As String) As Boolean
Try
Dim strLDAP As String = "LDAP://yourldapserverhere"
Dim m_obDirEntry As New
DirectoryEntry("GC://OU=users,OU=container,DC=domain,DC=domain,DC=com")
'This is the container root you want to start your search from
Dim srch As New DirectorySearcher(m_obDirEntry)

'This is where I have been having all my problems. strMember is
coming in as username, not DN, which is what I was originally trying to get
out. If you look at your AD setup, your users
'probably have their CN as the full name on the account, with their
actual username burried somewhere in that record, such as sAMAccountName,
which is the backwards compatable with Windows
'98 way of representing your username. Since I was searching for CN
(because I thought I had to), I was trying to get a DN out of a username and
it just wasn't working. Come to find out, I CAN
'search for something else - actually, I can search any darn thing
in there, which is nice and obvious now that I have done it...haha, so, I
switched out the expression below, which many recognize as
'the .Filter search string and stuck sAMAccountName in there instead
of CN and presto - I find the right user, now to enumerate the group
membership...

srch.Filter = "(&(objectClass=user)(sAMAccountName=" & strMember &
"))"

srch.PropertiesToLoad.Add("memberOf")

'Got this off the net someplace - kudos to the person that came up
with it. Now that I have the memberOf properties in srch (see line above),
I can build a string of JUST the group names (it originally
'comes out as one big mess, all DNs of the groups, that's not what I
want, hence the string builder...

Dim groupNames As New System.Text.StringBuilder
Dim result As SearchResult = srch.FindOne()
Dim propertyCount As Int32 = result.Properties("memberOf").Count
Dim dn As String
Dim equalsIndex As Int32, commaIndex As Int32
Dim propertyCounter As Int32
For propertyCounter = 0 To propertyCount - 1
dn = result.Properties("memberOf")(propertyCounter)
equalsIndex = dn.IndexOf("=", 1)
commaIndex = dn.IndexOf(",", 1)
If (-1 = equalsIndex) Then
groupNames.Append(dn)
Else
'This is where that magic happens, the author of this code
attaches just the group name to the end of the string and then inserts a
delimiter (could be anything almost, but a | is good)
groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex -
equalsIndex) - 1))
groupNames.Append("|")
End If
Next propertyCounter

'Now, if you look at my original calling code, you see that I really
DON'T want all of the groups, I just want to know if the group I gave it, is
in the username account of the username I gave, so it
'is really just a true or false I am looking for, so I take that
string and stick it into an array (there are other ways of doing it, I just
like this one today)

Dim ar As Array = Split(groupNames.ToString, "|")
Dim results As Boolean = False
For Each element As String In ar
If strGroup = element Then results = True
Next

'There you have it!
Return results

'Teardown stuff
m_obDirEntry.Dispose()
m_obDirEntry = Nothing
srch = Nothing
Catch ex As Exception
'Handle the exception
End Try
End Function


SO, after all that, I was finally able to take a username to an account in
AD, enumerate the groups and check if I was a member of said group. Hope
that is of some interest to you and anyone else thread watching.

:)

Derek
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top