我有这个代码:
public static DataTable ExecutesAMAccountNameQuery(string sAMAccountName)
{
string filter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=" + sAMAccountName + "))";
return ExecuteADQuery("GC:", filter);
}
它仅适用于完整的用户名,我不知道使其与通配符一起使用的语法,例如 sql 中的 LIKE?
It only works with the full username, I dont know the syntax to make it work with wildcards, like a LIKE in sql?
谢谢
如果您使用 .NET 3.5 或更新版本,您可以使用 PrincipalSearcher
和query-by-example"主体进行搜索:
If you're using .NET 3.5 or newer, you can use a PrincipalSearcher
and a "query-by-example" principal to do your searching:
// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// define a "query-by-example" principal - here, we search for a UserPrincipal
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.SamAccountName = "Esteban*";
// create your principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
// find all matches
foreach(var found in srch.FindAll())
{
// do whatever here - "found" is of type "Principal" - it could be user, group, computer.....
}
如果您还没有 - 绝对阅读 MSDN 文章管理目录安全主体在 .NET Framework 3.5 中,它很好地展示了如何充分利用 System.DirectoryServices.AccountManagement
中的新功能.或查看 System.DirectoryServices.AccountManagement 上的 MSDN 文档 命名空间.
If you haven't already - absolutely read the MSDN article Managing Directory Security Principals in the .NET Framework 3.5 which shows nicely how to make the best use of the new features in System.DirectoryServices.AccountManagement
. Or see the MSDN documentation on the System.DirectoryServices.AccountManagement namespace.
当然,根据您的需要,您可能希望为您创建的query-by-example"用户主体指定其他属性:
Of course, depending on your need, you might want to specify other properties on that "query-by-example" user principal you create:
DisplayName
(通常:名字 + 空格 + 姓氏)SAM 帐户名
- 您的 Windows/AD 帐户名用户主体名称
- 您的username@yourcompany.com"样式名称DisplayName
(typically: first name + space + last name)SAM Account Name
- your Windows/AD account nameUser Principal Name
- your "username@yourcompany.com" style name您可以在 UserPrincipal
上指定任何属性,并将其用作 PrincipalSearcher
的示例查询".
You can specify any of the properties on the UserPrincipal
and use those as "query-by-example" for your PrincipalSearcher
.
这篇关于使用通配符按 samaccountname 搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!