使用通配符按 samaccountname 搜索

search by samaccountname with wildcards(使用通配符按 samaccountname 搜索)
本文介绍了使用通配符按 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 name
  • User 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 搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

ActiveDirectory error 0x8000500c when traversing properties(遍历属性时 ActiveDirectory 错误 0x8000500c)
Get the list of Groups for the given UserPrincipal(获取给定 UserPrincipal 的组列表)
Can you find an Active Directory User#39;s Primary Group in C#?(你能在 C# 中找到 Active Directory 用户的主要组吗?)
Query From LDAP for User Groups(从 LDAP 查询用户组)
How can I get DOMAINUSER from an AD DirectoryEntry?(如何从 AD DirectoryEntry 获取 DOMAINUSER?)
Get List of Users From Active Directory In A Given AD Group(从给定 AD 组中的 Active Directory 获取用户列表)