-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActiveDirectory.cs
155 lines (136 loc) · 6.47 KB
/
ActiveDirectory.cs
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System.Collections.Generic;
using System.DirectoryServices.AccountManagement;
using System.Linq;
namespace ActiveDirectoryUtilities
{
public class ActiveDirectory: ActiveDirectoryOrganizationalUnit
{
// Name of the ActiveDirectory domain
public string DomainName { get; set; }
// Returns base path of Active directory
public string BasePath
{
get
{
return GetBasePath(DomainName);
}
}
// In order to instantiate this class, the user must pass these required values
public ActiveDirectory(string domainName, string operatingUsername, string operatingUserPassword)
: base(operatingUsername, operatingUserPassword, GetBasePath(domainName))
{
DomainName = domainName;
}
// Instance level methods that just call the static methods
public bool AuthenticateUser(string userName, string password)
{
return AuthenticateUser(DomainName, OperatingUsername,
OperatingUserPassword, userName, password);
}
public UserPrincipal GetUserByUserName(string userName)
{
return GetUserByUserName(DomainName, OperatingUsername,
OperatingUserPassword, userName);
}
public GroupPrincipal GetGroupByName(string groupName)
{
return GetGroupByName(DomainName, OperatingUsername,
OperatingUserPassword, groupName);
}
public ActiveDirectoryOrganizationalUnit GetOrganizationalUnit(string path = null, bool retrieveDescendants = false,
int? depth = null)
{
return GetOrganizationalUnit(DomainName, OperatingUsername,
OperatingUserPassword, path, retrieveDescendants, depth);
}
public List<GroupPrincipal> GetGroupsByName(string filter = "*")
{
return GetGroupsByName(DomainName, OperatingUsername,
OperatingUserPassword, filter);
}
public List<string> GetGroupNames(string filter = "*")
{
return GetGroupNames(DomainName, OperatingUsername,
OperatingUserPassword, filter);
}
// Authenticates a username/password combination against the specified domain
public static bool AuthenticateUser(string domainName, string operatingUsername, string operatingUserPassword, string userName, string password)
{
using (var domain = new PrincipalContext(ContextType.Domain, domainName, operatingUsername, operatingUserPassword))
{
return domain.ValidateCredentials(userName, password);
}
}
// Finds a user by their username. If no user is found, it returns null
public static UserPrincipal GetUserByUserName(string domainName, string operatingUsername, string operatingUserPassword, string userName)
{
try
{
var pc = new PrincipalContext(ContextType.Domain, domainName, operatingUsername, operatingUserPassword);
var u = UserPrincipal.FindByIdentity(pc, userName);
return u;
}
catch
{
return null;
}
}
// Finds a domain group by name. If no group is found, it returns null. The `groupName` may contain wildcards.
public static GroupPrincipal GetGroupByName(string domainName, string operatingUsername, string operatingUserPassword, string groupName)
{
var groups = GetGroupsByName(domainName, operatingUsername, operatingUserPassword, groupName);
if (!groups.Any()) return null;
var group = groups.First();
return group;
}
// Finds all domain groups by name. The `filter` may contain wildcards.
private static List<GroupPrincipal> GetGroupsByName(string domainName, string operatingUsername, string operatingUserPassword, string filter = "*")
{
var pc = new PrincipalContext(ContextType.Domain, domainName, operatingUsername, operatingUserPassword);
var group = new GroupPrincipal(pc) { Name = filter };
var searcher = new PrincipalSearcher { QueryFilter = @group };
var results = searcher.FindAll();
var rtn = results.Cast<GroupPrincipal>().ToList();
rtn = rtn.OrderBy(gp => gp.SamAccountName).ToList();
return rtn;
}
// Finds all organizational units and optionally their descendants.
// `path` is the ActiveDirectory path of the organization unit. If none is provided, the whole ActiveDirectory OU will be returned
// `retrieveDescendants` specifies whether or not the function should retrieve the descendants of the unit
// `depth` specifies how many levels of descendants the function should retrieve
public static ActiveDirectoryOrganizationalUnit GetOrganizationalUnit(string domainName, string operatingUsername, string operatingUserPassword,
string path = null, bool retrieveDescendants = false, int? depth = null)
{
var activeDirectory = new ActiveDirectory(domainName, operatingUsername, operatingUserPassword);
if (string.IsNullOrWhiteSpace(path))
{
path = null;
}
else
{
path = path.Trim('/');
if (!path.ToLower().Contains(activeDirectory.BasePath.ToLower()))
{
path = string.Format("{0}/{1}", activeDirectory.BasePath, path);
}
}
if (string.IsNullOrWhiteSpace(path))
{
return activeDirectory;
}
return new ActiveDirectoryOrganizationalUnit(operatingUsername, operatingUserPassword,
path, retrieveDescendants, depth);
}
// Only gets the names of the domain groups that match the filter passed
public static List<string> GetGroupNames(string domainName, string operatingUsername, string operatingUserPassword, string filter = "*")
{
var results = GetGroupsByName(domainName, operatingUsername, operatingUserPassword, filter);
return results.Select(principal => principal.SamAccountName).ToList();
}
// Build the Active Directory base URL
private static string GetBasePath(string domainName)
{
return string.Format("LDAP://{0}", domainName);
}
}
}