C# Active Directory中用于查询的PrincipalContext
我想从Active Directory制作一些简单的报告。经过讨论等,我发现如果我使用.NET FW 3.5及更高版本,使用C# Active Directory中用于查询的PrincipalContext,c#,active-directory,principalcontext,C#,Active Directory,Principalcontext,我想从Active Directory制作一些简单的报告。经过讨论等,我发现如果我使用.NET FW 3.5及更高版本,使用PrincipalContext是合适的。我想了解这个新功能的原理和我能做什么(不同于DirectoryEntry) 代码框架 // create your domain context PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN", "OU=SomeOU,
PrincipalContext
是合适的。我想了解这个新功能的原理和我能做什么(不同于DirectoryEntry
)
代码框架
// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain,
"YOURDOMAIN", "OU=SomeOU,DC=YourCompany,DC=com");
// define a "query-by-example" principal - here, we search for a UserPrincipal
// which has a password that will expire in 3 days or less
UserPrincipal userTemplate = new UserPrincipal(ctx);
userTemplate.AdvancedSearchFilter.AccountExpirationDate(DateTime.Today.AddDays(3), MatchType.LessThanOrEquals);
// instantiate searcher
PrincipalSearcher searcher = new PrincipalSearcher(userTemplate);
// enumerate matching users
foreach (Principal foundPrincipal in searcher.FindAll())
{
UserPrincipal foundUser = (foundPrincipal as UserPrincipal);
if (foundUser != null)
{
// do something with users found - e.g. send e-mail
}
}
可以通过代码向上添加此属性以登录到LDAP?:
- 使用什么LDAP(版本2或3)
- 如何设置运行LDAP的端口
- 如果我需要SSL连接,如何工作?(不同的端口,必须有特殊要求)
AdvancedSearchFilter
吗?(我只找到了
AccountExpirationDate
和AccountLockoutDate
)
- 用户密码将在不久的将来过期
- 用户密码已过期
- 检查用户的密码是否会过期
- 用户帐户过期(帐户,无密码)
- 过期用户帐户(帐户,无密码)
- 用户帐户未过期
如果您还没有阅读过MSDN文章,这篇文章很好地展示了如何最好地利用
System.DirectoryServices.AccountManagement
(我相信它回答了您的许多问题)您好,感谢您提供文章的链接,除了AD之外,我还阅读并尝试了测试代码,但不幸的是,我的处境仍然差不多。例如,当我锁定用户帐户时,“IsAccountLockedOut”属性的状态始终为False,如果我设置了帐户到期日期,则AccountLockoutTime属性永远不会设置,等等。此外,在UserPrincipal类中,许多其他属性根本不在用户下。我可能不明白为什么要用PrincipalContext而不是DirectoryEntry。对不起,我错了。错误会在椅子和键盘之间,我真的不懂原理。一旦我有了结果,我将发送正确的答案。