2014-07-29 4 views
0

Как я могу получить все группы из параметра memberof? Метод GetGroups возвращает член + первичная группа.GetGroups исключая основную группу

public static ArrayList GetUserGroups(string sUserName) 
{ 
    ArrayList myItems = new ArrayList(); 
    UserPrincipal oUserPrincipal = GetUser(sUserName); 

    PrincipalSearchResult<Principal> oPrincipalSearchResult = oUserPrincipal.GetGroups(); 

    foreach (Principal oResult in oPrincipalSearchResult) 
    { 
     if(NOT_PRIMARY) // <-- check by primary group ID? 
         // How get group GUID/ CID by ID ? 
      myItems.Add(oResult.Name); 
    } 
    return myItems; 
} 

ответ

0

Там решение:

public static ArrayList GetUserGroupsExcludingPrimaryGroup(string sUserName) 
    { 
     ArrayList myItems = new ArrayList(); 
     UserPrincipalEx oUserPrincipal = GetUser(sUserName); 

     var objectSid = oUserPrincipal.ObjectSid.ToString(); 
     objectSid = objectSid.Substring(0, objectSid.LastIndexOf("-")) + "-" + oUserPrincipal.PrimaryGroupID; 

     PrincipalSearchResult<Principal> oPrincipalSearchResult = oUserPrincipal.GetGroups(); 

     foreach (Principal oResult in oPrincipalSearchResult) 
     { 
      if (objectSid != oResult.Sid.ToString()) 
       myItems.Add(oResult.Name); 
     } 
     return myItems; 
    } 

AccountManagement - переопределить

namespace System.DirectoryServices.AccountManagement 
{ 
    [DirectoryRdnPrefix("CN")] 
    [DirectoryObjectClass("User")] 

    public class UserPrincipalEx : UserPrincipal 
    { 
     public UserPrincipalEx(PrincipalContext context) : base(context) { } 
     public UserPrincipalEx(PrincipalContext context, string samAccountName, string password, bool enabled) : base(context, samAccountName, password, enabled) { } 

     ... 

     [DirectoryProperty("primaryGroupID")] 
     public Int32 PrimaryGroupID 
     { 
      get { return (Int32)ExtensionGet("primaryGroupID")[0]; } 
      set { ExtensionSet("primaryGroupID", value); } 
     } 

     [DirectoryProperty("objectSid")] 
     public SecurityIdentifier ObjectSid 
     { 
      get { return new SecurityIdentifier((byte[])ExtensionGet("objectSid")[0], 0); } 
      set { ExtensionSet("objectSid", value); } 
     } 

    } 
}