Search code examples
securitysid

Determine if SID is User or Group


Is it possible to determine if a given SID is User or Group using .NET? I have a list of SIDs which I need to edit in a listview, so for User and Group I want to use different icons


Solution

  • You can try it by using System.DirectoryServices.AccountManagement:

    //Get NTAccount, to find out username and domen
    NTAccount nt = (NTAccount)sid.Translate(typeof(NTAccount));
    string[] fullName = nt.Value.Split(new char[] { '\\' });
    
    //then get group principle
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain, fullName[0]);
    GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.Name, fullName[1]);
    
    //and check whenever this group exists
    bool SidIsAGroup = grp != null;
    

    You can find similar question (and answer) here: How to get the groups of a user in Active Directory? (c#, asp.net)