2013-03-21 33 views
1

基本上,我發現,有我們在我們的應用程序有一個問題的解決方案後,將溶液:搜索本地組的本地用戶不具有外交與安全政策

private static void listGroupMembers(string groupDistinguishedName, PrincipalContext ctx, List<UserPrincipal> users) 
{ 
    DirectoryEntry group = new DirectoryEntry("LDAP://" + groupDistinguishedName); 
    foreach (string dn in group.Properties["member"]) 
    { 

     DirectoryEntry gpMemberEntry = new DirectoryEntry("LDAP://" + dn); 
     System.DirectoryServices.PropertyCollection userProps = gpMemberEntry.Properties; 

     object[] objCls = (userProps["objectClass"].Value) as object[]; 

     if (objCls.Contains("group")) 
      listGroupMembers(userProps["distinguishedName"].Value as string, ctx, users); 

     if (!objCls.Contains("foreignSecurityPrincipal")) 
     {      
      UserPrincipal u = UserPrincipal.FindByIdentity(ctx, IdentityType.DistinguishedName, dn); 
      if(u!=null) // u==null for any other types except users 
       users.Add(u); 
     } 
    }     
} 

然而我試圖搜索本地組,因此如果我更改目錄條目說:

DirectoryEntry groupEntry = 
      new DirectoryEntry(string.Format("WinNT://{0}/{1},group", Environment.MachineName, groupName)); 

然後它不起作用,它說該屬性不存在。我怎樣才能做到這一點,但對於一個本地組織和用戶呢?

回答

0

基本上解決這個問題我落得這樣做:

protected bool IsUserInLocalGroup(string userName, string group) 
    { 
     using (DirectoryEntry computerEntry = new DirectoryEntry("WinNT://{0},computer".FormatWith(Environment.MachineName))) 
     using(DirectoryEntry groupEntry = computerEntry.Children.Find(group, "Group")) 
     { 
      foreach (object o in (IEnumerable)groupEntry.Invoke("Members")) 
      { 
       using (DirectoryEntry entry = new DirectoryEntry(o)) 
       { 
        if (entry.SchemaClassName.Equals("User", StringComparison.OrdinalIgnoreCase) && entry.Name.Equals(userName, StringComparison.OrdinalIgnoreCase)) 
        { 
         return true; 
        } 
       } 
      } 
      return false; 
     } 
    }