Re: Getting the user's DN

From: Derek Martin (dmj2195_at_DONTSPAMMEokstateDOT.edu)
Date: 10/27/04


Date: Wed, 27 Oct 2004 09:36:15 -0500

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

"Steve Long" <Steve_Noneya@NoSpam.com> wrote in message
news:%23Ll1Lu6uEHA.1288@TK2MSFTNGP11.phx.gbl...
> would you mind sharing with me how you are doing that? I do find the
> problem
> interesting.
>
> Steve
>
> "Derek Martin" <dmj2195@DONTSPAMMEokstateDOT.edu> wrote in message
> news:%23YCmyu5uEHA.612@TK2MSFTNGP15.phx.gbl...
>> 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!
>>
>>
>>
>> "Steve Long" <Steve_Noneya@NoSpam.com> wrote in message
>> news:uHwP6$4uEHA.3972@TK2MSFTNGP10.phx.gbl...
>> > 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
>> >
>> > "Derek Martin" <dmj2195@DONTSPAMMEokstateDOT.edu> wrote in message
>> > news:%23XYUYavuEHA.2536@TK2MSFTNGP11.phx.gbl...
>> >> 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
>> >>
>> >>
>> >> "Ken Tucker [MVP]" <vb2ae@bellsouth.net> wrote in message
>> >> news:eC%23XNSuuEHA.4028@TK2MSFTNGP15.phx.gbl...
>> >> > Hi,
>> >> >
>> >> > Systeminformation.username, systeminformation.userdomain,
>> >> > environment.username, environment.userdomain
>> >> >
>> >> > Ken
>> >> > -------------------
>> >> > "Derek Martin" <dmj2195@DONTSPAMMEokstateDOT.edu> wrote in message
>> >> > news:uxoPentuEHA.684@TK2MSFTNGP10.phx.gbl...
>> >> > 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
>> >> >
>> >> > --
>> >> > Derek Martin
>> >> > 593074
>> >> >
>> >> >
>> >> >
>> >>
>> >>
>> >
>> >
>>
>>
>
>



Relevant Pages

  • Return Value?
    ... Public Class UserDetails ... Public FirstName As String ... Public Function Login(ByVal UserName As String, ... Dim sqlCmd As SqlCommand ...
    (microsoft.public.dotnet.framework.aspnet)
  • System.Security.Principal.WindowsIdentity file.copy
    ... Dim thePath As String 'source path ... Dim destPath As String 'destination path other system ... Private _username, _password, _domainname As String ... Public Sub New(ByVal username As String, ...
    (microsoft.public.dotnet.security)
  • System.Security.Principal.WindowsIdentity file.copy
    ... Dim thePath As String 'source path ... Dim destPath As String 'destination path other system ... Private _username, _password, _domainname As String ... Public Sub New(ByVal username As String, ...
    (microsoft.public.dotnet.security)
  • RE: Error Trap not working
    ... "Steve C" wrote: ... Dim ColAAddress As String, ColAName As String, ColBRange as String ... 'Code here that names the range of cells in Col. B as ColBRange ...
    (microsoft.public.excel.programming)
  • Re: Need help with N-Tier construction (Business layer)
    ... note that GetStoreID returns a DataTable instance, ... WHERE (StoreOwnerUserName = @UserName) ... Public Function GetStoreID(ByVal UserName As String) As DataTable ... Dim StoreDB As New ...
    (microsoft.public.dotnet.framework.aspnet)