日期:2014-05-17 浏览次数:20838 次
Active Directory(活动目录)是Windows Server 2003域环境中提供目录服务的组件。目录服务在微软平台上从Windows Server 2000开始引入,所以我们可以理解为活动目录是目录服务在微软平台的一种实现方式。当然目录服务在非微软平台上都有相应的实现。关于AD的更详细说明请上百度度娘吧,哈哈~
要在C#.net平台下实现AD相关操作,需要添加引用using System.DirectoryServices;
?
1.AD验证登入,登入窗体如下:
?
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; } }
?2.添加用户
?
?
?
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))