Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用通配符按samaccountname搜索_C#_Active Directory - Fatal编程技术网

C# 使用通配符按samaccountname搜索

C# 使用通配符按samaccountname搜索,c#,active-directory,C#,Active Directory,我有以下代码: public static DataTable ExecutesAMAccountNameQuery(string sAMAccountName) { string filter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=" + sAMAccountName + "))"; return ExecuteADQuery("GC:

我有以下代码:

 public static DataTable ExecutesAMAccountNameQuery(string sAMAccountName)
        {
            string filter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=" + sAMAccountName + "))";
            return ExecuteADQuery("GC:", filter);
        }
它只适用于完整的用户名,我不知道如何使用通配符,比如sql


谢谢

如果您使用的是.NET 3.5或更高版本,您可以使用PrincipalSearcher和按示例主体进行的查询来进行搜索:

// 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文章,这篇文章很好地展示了如何充分利用System.DirectoryServices.AccountManagement中的新功能。或者查看名称空间

当然,根据需要,您可能希望通过创建的示例用户主体指定该查询的其他属性:

DisplayName通常为:名字+空格+姓氏 SAM帐户名-您的Windows/AD帐户名 用户主体名称-您的username@yourcompany.com样式名
您可以在UserPrincipal上指定任何属性,并将其用作PrincipalSearcher的示例查询。

我只维护一些现有代码,所以我不想把您发送给我的代码搞得一团糟,不过,我想我可以将*放在开头和结尾,它也应该可以工作。例如:sAMAccountName=*+sAMAccountName+*@EstebanV.:它似乎应该可以工作,是的。这个答案显示.vbs脚本按sAMAccountName进行通配符查询。值得参考