注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

_

_

 
 
 

日志

 
 

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

2014-01-16 01:26:46|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
使用C#对Active Directory(活动目录)的操作

2012-07-31 15:04:07|? 分类: ASP.NET |? 标签:directoryservices 域操作 域用户 active directory |字号大中小 订阅

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

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

1.AD验证登入,登入窗体如下:

C#代码? 收藏代码

  1. private void Login_Click(object sender, EventArgs e)??? //登入按钮
  2. ?????? {?
  3. try
  4. ?????????? {?
  5. string FilterStr;//要查找的用户名??????????????
  6. ?????????????? DirectoryEntry entry = this.MyGetDirectoryEntry();?
  7. ?????????????? DirectorySearcher Seacher = new DirectorySearcher(entry);?
  8. ?????????????? FilterStr = "(&(objectClass=user) (cn=" + m_username + "))";?
  9. ?????????????? Seacher.Filter = FilterStr;?
  10. ?????????????? SearchResult Result = Seacher.FindOne();?
  11. if (Result == null)?
  12. ?????????????? {?
  13. ?????????????????? MessageBox.Show("用戶名或密碼有錯!");?
  14. ?????????????? }?
  15. else
  16. ?????????????? {?
  17. ?????????????????? MessageBox.Show("登入成功!");?
  18. ?????????????????? entry.AuthenticationType = AuthenticationTypes.Secure;?
  19. //this.Hide();
  20. //Form2 frm = new Form2();
  21. //frm.Show();
  22. ?????????????? }?
  23. ?????????? }?
  24. catch (Exception ex)?
  25. ?????????? {?
  26. string Str = ex.Message;?
  27. ?????????????? MessageBox.Show("用戶名或密碼有錯!");?
  28. ?????????? }?
  29. ?????? }?

C#代码? 收藏代码

  1. private void Check_Click(object sender, EventArgs e)?? //测试按钮
  2. ??? {?
  3. ??????????? m_username = UserName.Text;?
  4. if (UserExists(m_username))?
  5. ??????????? {?
  6. ??????????????? MessageBox.Show("連接成功!");?
  7. //? CreateNewUser("", "ezhrt1");
  8. ??????????? }?
  9. else
  10. ??????????? {?
  11. ??????????????? MessageBox.Show("連接失敗!");?
  12. ??????????? }?
  13. ??????? }?

C#代码? 收藏代码

  1. public bool UserExists(string UserName)?????? // 判断用户是否存在
  2. ??????? {?
  3. try
  4. ??????????? {?
  5. ??????????????? DirectoryEntry entry = this.MyGetDirectoryEntry();?
  6. ??????????????? DirectorySearcher Search = new DirectorySearcher();?
  7. ??????????????? Search.SearchRoot = entry;?
  8. ??????????????? Search.Filter = "(&(objectClass=user) (cn=" + UserName + "))";?
  9. ??????????????? SearchResultCollection results = Search.FindAll();?
  10. if (results.Count == 0)?
  11. ??????????????? {?
  12. return false;?
  13. ??????????????? }?
  14. else
  15. ??????????????? {?
  16. return true;?
  17. ??????????????? }?
  18. ??????????? }?
  19. catch (Exception ex)?
  20. ??????????? {?
  21. string Str = ex.Message;?
  22. return false;?
  23. ??????????? }?
  24. ??????? }?

2.添加用户

C#代码? 收藏代码

  1. private void button1_Click(object sender, EventArgs e)//测试
  2. ??????? {?
  3. ??????????? m_cn = Text_CN.Text;?
  4. if (this.UserExists(m_cn))?
  5. ??????????? {?
  6. ??????????????? LabelCheck.Text = "用户名已存在";?
  7. ??????????? }?
  8. else
  9. ??????????? {?
  10. ??????????????? LabelCheck.Text = "用户名可用";?
  11. ??????????? }?
  12. ??????? }?

C#代码? 收藏代码

  1. private void Login_Click(object sender, EventArgs e) //确定
  2. ?????? {?
  3. ?????????? m_cn = Text_CN.Text;?
  4. ?????????? m_bs = Text_Bname.Text;?
  5. ?????????? m_description = Text_Description.Text;?
  6. ?????????? m_givename = Text_GiveName.Text;?
  7. ?????????? m_mail = Text_Mail.Text;?
  8. ?????????? m_pass1 = Text_Pass1.Text;?
  9. ?????????? m_pass2 = Text_Pass2.Text;?
  10. ?????????? m_sn = Text_SN.Text;?
  11. ?????????? m_telephone = Text_Telephone.Text;?
  12. ?????????? m_www = Text_WWW.Text;?
  13. if (UserExists(m_cn))?
  14. ?????????? {?
  15. ?????????????? MessageBox.Show("用户名已存在!");?
  16. ?????????? }?
  17. else if (m_pass1.Equals("") || m_pass2.Equals("") || !m_pass1.Equals(m_pass2))?
  18. ?????????? {?
  19. ?????????????? MessageBox.Show("密码有误!");?
  20. ?????????? }?
  21. else if (IsEmail(m_mail))?
  22. ?????????? {?
  23. ?????????????? MessageBox.Show("邮箱地址格式不正确!");?
  24. ?????????? }?
  25. else
  26. ?????????? {?
  27. try
  28. ?????????????? {?
  29. ?????????????????? DirectoryEntry myEntry = new DirectoryEntry(m_bs, m_uname, m_pword, AuthenticationTypes.Secure);?
  30. //"LDAP://192.168.0.169/OU=eZHR,DC=Lanall,DC=com", "域管理用户", "域管理用户密码", AuthenticationTypes.Secure);
  31. ?????????????????? DirectoryEntries myEntries = myEntry .Children;?
  32. string Strname = "CN=" + m_cn;?
  33. ?????????????????? DirectoryEntry myDirectoryEntry = myEntries.Add(Strname, "user");?
  34. ?????????????????? myDirectoryEntry.Properties["userPrincipalName"].Value = m_sn + m_givename;?
  35. ?????????????????? myDirectoryEntry.Properties["name"].Value = m_givename;?
  36. ?????????????????? myDirectoryEntry.Properties["samAccountName"].Value = m_cn;?
  37. ?????????????????? myDirectoryEntry.Properties["pwdLastSet"].Value = -1;?
  38. ?????????????????? myDirectoryEntry.Properties["userAccountControl"].Value = 553;//553;// 66048; //590336;
  39. ?????????????????? myDirectoryEntry.Properties["sn"].Value = m_sn;?
  40. ?????????????????? myDirectoryEntry.Properties["givenName"].Value = m_givename;?
  41. ?????????????????? myDirectoryEntry.Properties["telephoneNumber"].Value = m_telephone;?
  42. ?????????????????? myDirectoryEntry.Properties["mail"].Value = m_mail;?
  43. ?????????????????? myDirectoryEntry.Properties["wWWHomePage"].Value = m_www;?
  44. ?????????????????? myDirectoryEntry.Properties["description"].Value = m_description;?
  45. ?????????????????? myDirectoryEntry.CommitChanges();?
  46. ?????????????????? myDirectoryEntry.Invoke("SetPassword", new object[] {m_pass1});?
  47. ?????????????????? MessageBox.Show("添加成功!");?
  48. ?????????????? }?
  49. catch (Exception ex)?
  50. ?????????????? {?
  51. string str = ex.Message;?
  52. ?????????????? }?
  53. ?????????? }?
  54. ?????? }?

C#代码? 收藏代码

  1. private void Del_Click(object sender, EventArgs e)//删除用户
  2. ?????? {???
  3. ?????????? m_cn = Text_CN.Text;?
  4. try
  5. ?????????? {?
  6. if (UserExists(m_cn))?
  7. ?????????????? {????????????????
  8. ?????????????????? DirectoryEntry entry = new DirectoryEntry(m_sname + m_bname, m_uname, m_pword);?
  9. ?????????????????? DirectorySearcher Search = new DirectorySearcher();?
  10. ?????????????????? Search.SearchRoot = entry;?
  11. ?????????????????? Search.Filter = "(&(objectClass=user) (cn=" + m_cn + "))";?
  12. ?????????????????? SearchResult Result = Search.FindOne();?
  13. ?????????????????? DirectoryEntry child = Result.GetDirectoryEntry();?
  14. ?????????????????? child.DeleteTree();?
  15. ?????????????????? MessageBox.Show("删除成功!");?
  16. ?????????????? }?
  17. else
  18. ?????????????? {?
  19. ?????????????????? MessageBox.Show("不存在用户:" + m_cn);?
  20. ?????????????? }?
  21. ?????????? }?
  22. catch (Exception ex)?
  23. ?????????? {?
  24. string Str = ex.Message;?
  25. ?????????? }?
  26. ?????? }?
  评论这张
 
阅读(283)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017