c# 3.0 - Can you find an Active Directory User's Primary Group in C#? -
I am working on an app that manages the user account in Active Directory. I am using the System.DirectoryServices.AccountManagement namespace as much as possible, but I can not figure out how to determine the user's primary group. When I try to remove the group that is the primary group of the user, I get an exception. Here's my current code:
Private Zero Leading Group (Userprinit Userprinus, Group Pinyith Group Principal) {TODO: See to see if this group is the primary group of users or not. GroupPrincipal.Members.Remove (userPrincipal); GroupPrincipal.Save (); }
Is there a way to get the name of the user's primary group, so can I do some assumptions before trying to delete a user from this group?
Private string GetPrimaryGroup (DirectoryEntryEntry, DirectoryEntryDomainEntry) {int primaryGroupID = (int) aEntry.Properties ["PrimaryGroupID"]. ; Byte [] ObjectSid = (Byte []) An Entry. Properties ["Objectsid"]. Values; Stringbuilder savedGreasecid = new stringbilder (); // Copy everything but final four bytes (sub authorization) // by doing so (usit I = 0; I & lt; Objectsid.Lang - 4; I ++) {escapedGroupSid.AppendFormat ("\\ {0 : X2} ", objectcid [ii]); } // In order to create primary group SID of Primary Group ID, add the escape string (UIT I = 0; I and LT; 4; i ++) {escapedGroupSid.AppendFormat ("\\ {0: x2}", (Primary Group ID & amp; 0xFF); Primary Group ID & gt; & Gt; = 8; } // Find the directory for the group with this SID directory explorer explorer = new directory finder (); If (Admin Entry! = Null) {searcher.SearchRoot = aDomainEntry; } Explorers Filter = "(and (object category = group) (object AIDD =" + escaped group cid. ToString (+ +) ");"; Searcher.PropertiesToLoad.Add ("distinguishedName"); Return finder.fundon (). Properties ["specific name"] [0]. Ostring (); }
Hope this helps.
Mark
Comments
Post a Comment