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 搜索
- 在 C# 中异步处理项目队列 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 使用 rss + c# 2022-01-01
