Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try something like this maybe:</p> <p>Declare Static List of Group Objects (simple class of GroupPrincipal, integer level, and parent GroupPrincipal)</p> <pre><code>public class SomeDirTraverser { private static List&lt;GroupObj&gt; _groups = new List&lt;GroupObj&gt;(); public List&lt;string&gt; GetMembershipWithPath(string groupname) { List&lt;string&gt; retVal = new List&lt;string&gt;(); PrincipalContext ctx = new PrincipalContext(ContextType.Domain); GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.Name, groupname); if (grp != null) { BuildHList(grp, 0, null); foreach (UserPrincipal usr in grp.GetMembers(true)) retVal.Add(GetMbrPath(usr)); } return retVal; } private void BuildHList(GroupPrincipal node, int level, GroupPrincipal parent) { PrincipalSearchResult&lt;Principal&gt; rslts = node.GetMembers(); _groups.Add(new GroupObj() { Group = node, Level = level, Parent = parent }); foreach (GroupPrincipal grp in rslts.Where(g =&gt; g is GroupPrincipal)) BuildHList(grp, level + 1, node); } private string GetMbrPath(UserPrincipal usr) { Stack&lt;string&gt; output = new Stack&lt;string&gt;(); StringBuilder retVal = new StringBuilder(); GroupObj fg = null, tg = null; output.Push(usr.Name); foreach (GroupObj go in _groups) { if (usr.IsMemberOf(go.Group)) { output.Push(go.Group.Name); fg = go; while (fg.Parent != null) { output.Push(fg.Parent.Name); tg = (from g in _groups where g.Group == fg.Parent select g).FirstOrDefault(); fg = tg; } break; } } while (output.Count &gt; 1) retVal.AppendFormat("{0} -&gt;", output.Pop()); retVal.Append(output.Pop()); return retVal.ToString(); } } public class GroupObj { public GroupPrincipal Group { get; set; } public int Level { get; set; } public GroupPrincipal Parent { get; set; } } </code></pre> <p>This one looks like it should give you what you want.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload