2

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

javros
  • 825
  • 9
  • 31

2 Answers2

1

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)

Community
  • 1
  • 1
JleruOHeP
  • 8,924
  • 2
  • 36
  • 65
0

LookupAccountSid() function returns SID_NAME_USE value that indicates the type of the account.

Brian Cannard
  • 768
  • 7
  • 19