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

系统 1911 0
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;
            }
        }

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


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论