要在C#.net平臺下實(shí)現(xiàn)AD相關(guān)操作,需要添加引用using System.DirectoryServices;
??????? private void Login_Click(object sender, EventArgs e)??? //登入按鈕
運(yùn)費(fèi)
??????? {
??????????? 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;
??????????? }
??????? }
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
