Search code examples
asp.netvb.netactive-directoryusergroupssid

VB.NET - How to Convert SID to Group Name with Active Directory


Using VB.NET, How do you Convert the sid to Group Name with Active Directory?

example: I need to get "group_test" and not "S-1-5-32-544"

The code I'm using is:

Public ReadOnly Property Groups As IdentityReferenceCollection
    Get

        Dim irc As IdentityReferenceCollection
        Dim ir As IdentityReference
        irc = WindowsIdentity.GetCurrent().Groups
        Dim strGroupName As String

        For Each ir In irc
            Dim mktGroup As IdentityReference = ir.Translate(GetType(NTAccount))
            MsgBox(mktGroup.Value)
            Debug.WriteLine(mktGroup.Value)
            strGroupName = mktGroup.Value.ToString

        Next

        Return irc

    End Get
End Property

or something like this?

        currentUser = WindowsIdentity.GetCurrent()

        For Each refGroup As IdentityReference In currentUser.Groups

            Dim acc As NTAccount = TryCast(refGroup.Translate(GetType(NTAccount)), NTAccount)
            If AdminGroupName = acc.Value Then
                ret = "999"
            End If
            If UsersGroupName = acc.Value Then
                ret = "1"
            End If

how would u adapt it to this code? (if user is in xx group, show xx group on drop down list)

        For Each UserGroup In WindowsIdentity.GetCurrent().Groups
            If mktGroup.Value = "BIG" Then
                Dim Company = ac1.Cast(Of MarketingCompany).Where(Function(ac) ac.MarketingCompanyShort = "BIG").FirstOrDefault
                If Company IsNot Nothing Then
                    marketingCo.Items.Add(String.Format("{0} | {1}", Company.MarketingCompanyShort, Company.MarketingCompanyName))
                End If
            End If
        Next

Solution

  • Here is a simple way writen in C#, I think it's not to hard to adapt :

      /* Retreiving object from SID
      */
      string SidLDAPURLForm = "LDAP://WM2008R2ENT:389/<SID={0}>";
      System.Security.Principal.SecurityIdentifier sidToFind = new System.Security.Principal.SecurityIdentifier("S-1-5-21-3115856885-816991240-3296679909-1106");
    
      DirectoryEntry userEntry = new DirectoryEntry(string.Format(SidLDAPURLForm, sidToFind.Value));
    
      string name = userEntry.Properties["cn"].Value.ToString();
    

    Here it is in VB .NET thanks to REFLECTOR

    Dim SidLDAPURLForm As String = "LDAP://WM2008R2ENT:389/<SID={0}>"
    Dim sidToFind As New SecurityIdentifier("S-1-5-21-3115856885-816991240-3296679909-1106")
    Dim userEntry As New DirectoryEntry(String.Format(SidLDAPURLForm, sidToFind.Value))
    Dim name As String = userEntry.Properties.Item("cn").Value.ToString
    

    ---- EDITED ----- So here is what you wants, but it's the same as that was previously given by @BiggsTRC

    Private Shared Sub Main(args As String())
        Dim currentUser As WindowsIdentity = WindowsIdentity.GetCurrent()
    
    For Each iRef As IdentityReference In currentUser.Groups
            Console.WriteLine(iRef.Translate(GetType(NTAccount)))
        Next
    End Sub