search by samaccountname with wildcards(使用通配符按 samaccountname 搜索)
问题描述
我有这个代码:
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 搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用通配符按 samaccountname 搜索
基础教程推荐
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
