使用C#对Active Directory(活动目录)的操作

Active Directory(活动目录)是Windows Server 2003域环境中提供目录服务的组件。目录服务在微软平台上从Windows Server 2000开始引入,所以我们可以理解为活动目录是目录服务在微软平台的一种实现方式。当然目录服务在非微软平台上都有相应的实现。关于AD的更详细说明请 上百度度娘吧,哈哈~

要在C#.net平台下实现AD相关操作,需要添加引用using System.DirectoryServices;

        private void Login_Click(object sender, EventArgs e)    //登入按钮运费
        {
            try
            {
                string FilterStr;//要查找的用户名              
                DirectoryEntry entry = this.MyGetDirectoryEntry();
                DirectorySearcher Seacher = new DirectorySearcher(entry);
                FilterStr = "(&(objectClass=user) (cn=" + m_username + "))";
                Seacher.Filter = FilterStr;
                SearchResult Result = Seacher.FindOne();
                if (Result == null)
                {
                    MessageBox.Show("用戶名或密碼有錯!");
                }
                else
                {
                    MessageBox.Show("登入成功!");
                    entry.AuthenticationType = AuthenticationTypes.Secure;
                    //this.Hide();
                    //Form2 frm = new Form2();
                    //frm.Show();
                }
            }
            catch (Exception ex)
            {
                string Str = ex.Message;
                MessageBox.Show("用戶名或密碼有錯!");
            }
        }

    private void Check_Click(object sender, EventArgs e)   //测试按钮
    {
            m_username = UserName.Text;
            if (UserExists(m_username))
            {女装品牌排行榜
                MessageBox.Show("連接成功!");
               //  CreateNewUser("", "ezhrt1");
            }
            else
            {
                MessageBox.Show("連接失敗!");
            }
        }

        public bool UserExists(string UserName)       // 判断用户是否存在
        {
            try
            {
                DirectoryEntry entry = this.MyGetDirectoryEntry();
                DirectorySearcher Search = new DirectorySearcher();
                Search.SearchRoot = entry;
                Search.Filter = "(&(objectClass=user) (cn=" + UserName + "))";
                SearchResultCollection results = Search.FindAll();
                if (results.Count == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                string Str = ex.Message;
                return false;
            }
        }

        private void button1_Click(object sender, EventArgs e)//测试
        {
            m_cn = Text_CN.Text;
            if (this.UserExists(m_cn))
            {
                LabelCheck.Text = "用户名已存在";
            }
            else
            {
                LabelCheck.Text = "用户名可用";
            }
        }

        private void Login_Click(object sender, EventArgs e) //确定
        {
            m_cn = Text_CN.Text;
            m_bs = Text_Bname.Text;
            m_description = Text_Description.Text;
            m_givename = Text_GiveName.Text;
            m_mail = Text_Mail.Text;
            m_pass1 = Text_Pass1.Text;
            m_pass2 = Text_Pass2.Text;
            m_sn = Text_SN.Text;
            m_telephone = Text_Telephone.Text;
            m_www = Text_WWW.Text;
            if (UserExists(m_cn))
            {
                MessageBox.Show("用户名已存在!");
            }
            else if (m_pass1.Equals("") || m_pass2.Equals("") || !m_pass1.Equals(m_pass2))
            {
                MessageBox.Show("密码有误!");
            }
            else if (IsEmail(m_mail))
            {
                MessageBox.Show("邮箱地址格式不正确!");
            }
            else
            {
                try
                {
                    DirectoryEntry myEntry = new DirectoryEntry(m_bs, m_uname, m_pword, AuthenticationTypes.Secure);
                       
                    //"LDAP://192.168.0.169/OU=eZHR,DC=Lanall,DC=com", "域管理用户", "域管理用户密码", AuthenticationTypes.Secure);
                    DirectoryEntries myEntries = myEntry .Children;
                    string Strname = "CN=" + m_cn;
                    DirectoryEntry myDirectoryEntry = myEntries.Add(Strname, "user");
                    myDirectoryEntry.Properties["userPrincipalName"].Value = m_sn + m_givename;
                    myDirectoryEntry.Properties["name"].Value = m_givename;
                    myDirectoryEntry.Properties["samAccountName"].Value = m_cn;
                    myDirectoryEntry.Properties["pwdLastSet"].Value = -1;
                    myDirectoryEntry.Properties["userAccountControl"].Value = 553;//553;// 66048; //590336;
                    myDirectoryEntry.Properties["sn"].Value = m_sn;
                    myDirectoryEntry.Properties["givenName"].Value = m_givename;
                    myDirectoryEntry.Properties["telephoneNumber"].Value = m_telephone;
                    myDirectoryEntry.Properties["mail"].Value = m_mail;
                    myDirectoryEntry.Properties["wWWHomePage"].Value = m_www;
                    myDirectoryEntry.Properties["description"].Value = m_description;
                   
                    myDirectoryEntry.CommitChanges();
                    myDirectoryEntry.Invoke("SetPassword", new object[] {m_pass1});
                    MessageBox.Show("添加成功!");
                }
                catch (Exception ex)
                {
                    string str = ex.Message;
                }
            }
          
        }

        private void Del_Click(object sender, EventArgs e)//删除用户
        { 
            m_cn = Text_CN.Text;
            try
            {
                if (UserExists(m_cn))
                {              
                    DirectoryEntry entry = new DirectoryEntry(m_sname + m_bname, m_uname, m_pword);
                    DirectorySearcher Search = new DirectorySearcher();
                    Search.SearchRoot = entry;
                    Search.Filter = "(&(objectClass=user) (cn=" + m_cn + "))";
                    SearchResult Result = Search.FindOne();
                    DirectoryEntry child = Result.GetDirectoryEntry();
                    child.DeleteTree();
                    MessageBox.Show("删除成功!");
                }
                else
                {
                    MessageBox.Show("不存在用户:" + m_cn);
                }
            }
            catch (Exception ex)
            {
                string Str = ex.Message;
            }
        }

你可能感兴趣的:(Directory)