我有一个应用程序使用ActiveDirectory授权,现决定需要支持嵌套的AD组,例如:
MAIN_AD_GROUP
|
|-> SUB_GROUP
|
|-> User
所以,用户并非直接成员MAIN_AD_GROUP
。我希望能够递归查找搜索嵌套在MAIN_AD_GROUP
中的组来查找用户。主要问题是我正在使用.NET 3.5,并且在.NET 3.5中的
System.DirectoryServices.AccountManagement
存在一个错误,即UserPrincipal.IsMemberOf()
方法无法为具有超过1500个用户的组工作。因此,我无法使用UserPrincipal.IsMemberOf()
方法,也不能切换到.NET 4。我通过以下函数解决了这个问题:
private bool IsMember(Principal userPrincipal, Principal groupPrincipal)
{
using (var groups = userPrincipal.GetGroups())
{
var isMember = groups.Any(g =>
g.DistinguishedName == groupPrincipal.DistinguishedName);
return isMember;
}
}
userPrincipal.GetGroups()
只返回用户是直接成员的组。
如何使用嵌套组使其工作?
DirectoryEntry domainConnection
,通过传递在不同域上有效的用户名和密码。请参阅:http://stackoverflow.com/questions/9362724/cross-domain-authentication-using-directoryservices - Tim LewisDirectoryEntry domainConnection = new DirectoryEntry("LDAP://example.com", "username", "password"); // Use this to query a remote domain
- Tim Lewis