Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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# 在Active Directory中查询OU中的所有用户,并将用户名输出到listbox_C#_C# 4.0_Active Directory - Fatal编程技术网

C# 在Active Directory中查询OU中的所有用户,并将用户名输出到listbox

C# 在Active Directory中查询OU中的所有用户,并将用户名输出到listbox,c#,c#-4.0,active-directory,C#,C# 4.0,Active Directory,我需要修改我们添加到模式中的一个自定义属性,但是要基于所有用户。该属性是一个MD5散列,我已经将其存储为一个公共变量。我正在尝试获取要在列表框中列出的指定OU中所有用户的列表,以便您可以选择要应用值的所有用户或单个用户 这是我当前的Form1.cs代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using

我需要修改我们添加到模式中的一个自定义属性,但是要基于所有用户。该属性是一个MD5散列,我已经将其存储为一个公共变量。我正在尝试获取要在列表框中列出的指定OU中所有用户的列表,以便您可以选择要应用值的所有用户或单个用户

这是我当前的Form1.cs代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.DirectoryServices;



namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        String Password;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            Password = textBox1.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bs = System.Text.Encoding.UTF8.GetBytes(Password);
            bs = x.ComputeHash(bs);
            System.Text.StringBuilder s = new System.Text.StringBuilder();
            foreach (byte b in bs)
            {
                s.Append(b.ToString("x2").ToLower());
            }
            Password = s.ToString();

            textBox2.Text = Password;


        }   

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

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

// List of strings for your names
List<string> allUsers = new List<string>();

// create your domain context and define the OU container to search in
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "DOMAINNAME", 
                                            "OU=SomeOU,dc=YourCompany,dc=com");

// define a "query-by-example" principal - here, we search for a UserPrincipal (user)
UserPrincipal qbeUser = new UserPrincipal(ctx);

// 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.....          
   allUsers.Add(found.DisplayName);
}
//名称的字符串列表
List allUsers=新列表();
//创建域上下文并定义要在其中搜索的OU容器
PrincipalContext ctx=新PrincipalContext(ContextType.Domain,“域名”,
“OU=SomeOU,dc=YourCompany,dc=com”);
//定义一个“示例查询”主体-在这里,我们搜索一个UserPrincipal(用户)
UserPrincipal qbeUser=新的UserPrincipal(ctx);
//创建传递QBE主体的主体搜索者
PrincipalSearcher srch=新PrincipalSearcher(qbeUser);
//查找所有匹配项
foreach(在srch.FindAll()中找到的变量)
{
//在这里做任何事情-“发现”属于“主体”类型-可能是用户、组、计算机。。。。。
allUsers.Add(find.DisplayName);
}
如果您还没有-请阅读MSDN文章,这篇文章很好地展示了如何最好地利用
System.DirectoryServices.AccountManagement中的新功能


您可以指定
UserPrincipal
上的任何属性,并将其用作
PrincipalSearcher
的“示例查询”

如何获取所有用户,不仅是特定用户,而且是所有用户,并将这些信息获取到一个数组中,或者可以在列表框中显示的某个内容。因此,是“ctx”我搜索用户的值是多少?此外,如何将找到的值放入数组或可以放入列表框的内容中?thanks@JeffClay:更新了我的回复-您基本上需要为容器中找到的每个用户输入一些信息(我选择了“DisplayName”),例如
列表
,然后将其绑定到您的ListBoxAwest,谢谢。还有一件事。。。如何与我的广告服务器绑定以查询此信息?@Jeff Clay:“绑定”发生在创建
PrincipalContext
时。如果需要,可以在
PrincipalContext
的构造函数中指定用户名/密码。