Question

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

Was it helpful?

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)

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top