-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathActiveDirectory-Lookup.linq
More file actions
125 lines (105 loc) · 2.93 KB
/
ActiveDirectory-Lookup.linq
File metadata and controls
125 lines (105 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<Query Kind="Program">
<Reference><RuntimeDirectory>\System.DirectoryServices.AccountManagement.dll</Reference>
<Reference><RuntimeDirectory>\System.DirectoryServices.dll</Reference>
<Reference><RuntimeDirectory>\System.Configuration.dll</Reference>
<NuGetReference>System.DirectoryServices.AccountManagement</NuGetReference>
<Namespace>System.DirectoryServices</Namespace>
<Namespace>System.DirectoryServices.ActiveDirectory</Namespace>
<Namespace>System.DirectoryServices.AccountManagement</Namespace>
</Query>
void Main()
{
var result = ActiveDirectoryHelper.GetCurrentDomains();
var df = ActiveDirectoryHelper.GetUserGroups("user1", result);
var df2 = ActiveDirectoryHelper.GetUserGroups("user2", result);
df.Dump();
df2.Dump();
}
// Define other methods and classes here
public static class ActiveDirectoryHelper
{
public static IEnumerable<string> GetCurrentDomains()
{
var domains = new List<string>();
using (var forest = Forest.GetCurrentForest())
{
foreach (Domain domain in forest.Domains)
{
domains.Add(domain.Name);
domain.Dispose();
}
}
return domains;
}
public static IEnumerable<string> GetUserGroups(string username, IEnumerable<string> domains)
{
return domains.AsParallel().Select(x => GetUserGroups(username, x)).SelectMany(y => y);
}
public static IEnumerable<string> GetUserGroups(string userName, string domain)
{
var groups = new List<string>();
PrincipalContext cbx = new PrincipalContext(ContextType.Domain, domain);
UserPrincipal user = UserPrincipal.FindByIdentity(cbx, userName);
var groupResults = user?.GetGroups() as PrincipalSearchResult<Principal>;
if (groupResults is PrincipalSearchResult<Principal>)
{
foreach (Principal p in groupResults)
{
groups.Add(p.Name);
}
}
return groups;
}
public static IEnumerable<string> GetDomainGroups(string domainName)
{
var groups = new List<string>();
DirectoryEntry ADAM = default(DirectoryEntry);
DirectoryEntry GroupEntry = default(DirectoryEntry);
DirectorySearcher SearchAdam = default(DirectorySearcher);
SearchResultCollection SearchResults = default(SearchResultCollection);
var result = new List<string>();
try
{
ADAM = new DirectoryEntry($"LDAP://{domainName}");
ADAM.RefreshCache();
}
catch (Exception e)
{
throw e;
}
try
{
SearchAdam = new DirectorySearcher(ADAM)
{
Filter = "(&(objectClass=group))",
SearchScope = SearchScope.Subtree
};
SearchResults = SearchAdam.FindAll();
}
catch (Exception e)
{
throw e;
}
try
{
if (SearchResults.Count != 0)
{
foreach (SearchResult objResult in SearchResults)
{
GroupEntry = objResult.GetDirectoryEntry();
int a = GroupEntry.Name.Trim().IndexOf("=".ToString());
groups.Add(GroupEntry.Name.Trim().Substring(a + 1));
}
}
else
{
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
groups.Sort();
return groups;
}
}