已阅读5页,还剩12页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.DirectoryServices;using System.Text.RegularExpressions;using System.Data;using System.Collections;namespace AD测试 class ADhelper private string path; private string username; private string password; public ADhelper(string Path, string Username, string Password) path = Path; username = Username; password = Password; / / 创建AD连接 / / public DirectoryEntry GetDirectoryEntry() try DirectoryEntry de = new DirectoryEntry(); de.Path = path; /example:LDAP:/qjyczsgl/CN=Users,DC=qjyczsgl,DC=com de.Username = username; de.Password = password; return de; catch return null; / / 判断对象是否存在 / / / public bool Exists(string objectPath) bool found = false; if (DirectoryEntry.Exists(LDAP:/ + objectPath) found = true; return found; / / 判断用户是否存在 / / / public bool UserExists(string UserName) try DirectoryEntry de = this.GetDirectoryEntry(); DirectorySearcher deSearch = new DirectorySearcher(); deSearch.SearchRoot = de; deSearch.Filter = (&(objectCategory=Person)(objectClass=user)(sAMAccountName= + UserName + );/CN和sAMAccountName有区别,CN是全名、在服务器做查询的时候显示出来的名称,sAMAccountName是登录名(暂定这样认为) /SearchResultCollection results = deSearch.FindAll(); SearchResult result = deSearch.FindOne(); if (result != null) return true; else return false; catch return false; / / 查询出用户 / / / public DirectoryEntry UserSelect(string UserName) try DirectoryEntry de = this.GetDirectoryEntry(); DirectorySearcher deSearch = new DirectorySearcher(); deSearch.SearchRoot = de; deSearch.Filter = (&(objectCategory=Person)(objectClass=user)(sAMAccountName= + UserName + ); /SearchResultCollection results = deSearch.FindAll(); SearchResult result = deSearch.FindOne(); if (result != null) return result.GetDirectoryEntry(); else return null; catch return null; / / 查询出用户2,使用用户的path、UserName、PassWord创建一个DirectoryEntry类事例,以此判断用户输入密码是否正确 / / / / / public DirectoryEntry UserSelect(string path,string UserName,string PassWord) try DirectoryEntry de = new DirectoryEntry(path,UserName,PassWord); if (de.Path != null) return de; else return null; catch return null; / / 查询出所有用户 / / / public SearchResultCollection User() try DirectoryEntry de = this.GetDirectoryEntry(); DirectorySearcher deSearch = new DirectorySearcher(); deSearch.SearchRoot = de; deSearch.Filter = (&(objectCategory=Person)(objectClass=user); SearchResultCollection results = deSearch.FindAll(); /SearchResult result = deSearch.FindOne(); if (results.Count 0) return results; else return null; catch return null; #region 创建一个用户(步骤) / / 创建一个用户,放在域根目录下 / / / public DirectoryEntry CreateNewUser(string UserName) try DirectoryEntry de = this.GetDirectoryEntry(); DirectoryEntries users = de.Children; DirectoryEntry newuser = users.Add(CN= + UserName, user); newuser.CommitChanges(); return newuser; catch return null; / / 创建一个用户,放在指定的初始位置下 / / / public DirectoryEntry CreateNewUser(string ldapPath, string UserName) try string connectionPrefix = LDAP:/ + ldapPath; DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix); DirectoryEntry newUser = dirEntry.Children.Add(CN= + UserName, user); newUser.CommitChanges(); dirEntry.Close(); return newUser; catch(System.DirectoryServices.DirectoryServicesCOMException E) return null; / / 修改用户属性 / / / / / public bool SetProperty(DirectoryEntry newuser, string PropertyName, string PropertyValue) try if (PropertyValue != null) if (newuser.Properties.Contains(PropertyName) newuser.PropertiesPropertyName0 = PropertyValue; /newuser.PropertiesPropertyName.Value = PropertyValue; else newuser.PropertiesPropertyName.Add(PropertyValue); newuser.CommitChanges(); return true; catch return false; / / 设置用户新密码,重置密码 / / / / public bool SetPassword(DirectoryEntry newuser, string Password) try newuser.AuthenticationType = AuthenticationTypes.Secure; object PassWord = new object Password ; object ret = newuser.Invoke(SetPassword, PassWord); newuser.CommitChanges(); newuser.Close(); return true; catch return false; / / 修改用户密码 / / / / public bool ChangePassword(DirectoryEntry newuser, string OldPassword,string NewPassword) try /newuser.AuthenticationType = AuthenticationTypes.Secure; object PassWord = new object OldPassword, NewPassword ; object ret = newuser.Invoke(ChangePassword, PassWord); newuser.CommitChanges(); newuser.Close(); return true; catch return false; / / 启用用户帐号 / / / public bool EnableAccount(DirectoryEntry newuser) try int val = (int)newuser.PropertiesuserAccountControl.Value; newuser.PropertiesuserAccountControl.Value = val & 0x2; newuser.CommitChanges(); return true; catch return false; / / 禁用一个帐号 / / / public bool DisableAccount(string UserName) try DirectoryEntry de = this.GetDirectoryEntry(); DirectorySearcher ds = new DirectorySearcher(de); ds.Filter = (&(objectCategory=Person)(objectClass=user)(sAMAccountName= + UserName + ); ds.SearchScope = SearchScope.Subtree; SearchResult results = ds.FindOne(); if (results != null) DirectoryEntry dey = results.GetDirectoryEntry(); int val = (int)dey.PropertiesuserAccountControl.Value; dey.PropertiesuserAccountControl.Value = val | 0x2;/0x0002 dey.CommitChanges(); dey.Close(); de.Close(); return true; else de.Close(); return false; catch return false; / / 添加用户到组 / / / / / public bool AddUserToGroup(DirectoryEntry de, DirectoryEntry newuser, string GroupName) try DirectorySearcher deSearch = new DirectorySearcher(); deSearch.SearchRoot = de; deSearch.Filter = (&(objectClass=group) (cn= + GroupName + ); SearchResultCollection results = deSearch.FindAll(); bool isGroupMember = false; if (results.Count 0) DirectoryEntry group = results0.GetDirectoryEntry();/ this.GetDirectoryEntry(results0.Path); object members = group.Invoke(Members, null); if (group.Propertiesmember.Count 0)/解决组中没有成员时产生BUG的问题 foreach (object member in (IEnumerable)members) DirectoryEntry x = new DirectoryEntry(member); if (x.Name != newuser.Name) isGroupMember = true; else isGroupMember = false; break; else isGroupMember = true; if (isGroupMember) group.Invoke(Add, new object newuser.Path.ToString() ); /group.Propertiesmember.Add(newuser.Path.ToString(); group.CommitChanges(); group.Close(); return true; group.Close(); return false; catch return false; / / 从用户组删除用户 / / / / / public bool DeleteUserFromGroup(DirectoryEntry de, DirectoryEntry user, string GroupName) try DirectorySearcher deSearch = new DirectorySearcher(); deSearch.SearchRoot = de; deSearch.Filter = (&(objectClass=group) (cn= + GroupName + ); SearchResultCollection results = deSearch.FindAll(); bool isGroupMember = false; if (results.Count 0) DirectoryEntry group = results0.GetDirectoryEntry();/ this.GetDirectoryEntry(results0.Path); object members = group.Invoke(Members, null); foreach (object member in (IEnumerable)members) DirectoryEntry x = new DirectoryEntry(member); if (x.Name != user.Name) isGroupMember = false; else isGroupMember = true; break; if (isGroupMember) group.Invoke(Remove, new object user.Path.ToString() ); /group.Propertiesmember.Remove(object)user.Path.ToString(); group.CommitChanges(); group.Close(); return true; group.Close(); return false; catch return false; / / 将对象从现有位置移到新的位置 / / / / public bool MoveLocation(string objectLocation, string newLocation) /It should be noted that the string newLocation should NOT include the CN= value of the object. /the objectLocation is something like: CN=group,OU=GROUPS,DC=contoso,DC=com. /the newLocation is something like: OU=NewOUParent,DC=contoso,DC=com. try /if(Exists() DirectoryEntry eLocation = new DirectoryEntry(LDAP:/ + objectLocation); DirectoryEntry nLocation = new DirectoryEntry(LDAP:/ + newLocation); /string newName = eLocation.Name; /eLocation.MoveTo(nLocation, newName); eLocation.MoveTo(nLocation); nLocation.Close(); eLocation.Close(); return true; catch return false; #endregion #region 创建用户 / / 创建一个新用户 / / / / / public bool CreateNewUser(string userName, string loginName, string password, string groupName) try /Catalog catalog = new Catalog(); DirectoryEntry de = this.GetDirectoryEntry(); / 1. Create user account DirectoryEntry newuser = this.CreateNewUser(userName); / 2. Set properties SetProperty(newuser, SAMAccountName, loginName); SetProperty(newuser, displayName, userName); / 3. Set password SetPassword(newuser, password); /newuser.CommitChanges(); / 4. Enable account EnableAccount(newuser); / 5. Add user account to groups AddUserToGroup(de,
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 人教版新教材七年级上册英语时文组合训练(二)教学设计
- 初中综合实践活动九年级“传统节日文化探究与体验”教学设计
- 初中美术八年级下册第七单元《乡村展新貌-乡村改造规划师》教学设计
- 小学四年级语文下册说明性与叙事性语段结构辨认及概括专项训练教学设计
- 高三历史一轮复习教学设计:统编版教材与湖南考情的精准对接
- 小学语文五年级下册第七单元习作中国的世界文化遗产教学设计
- 高二信息技术《了解数据管理技术》教学设计
- 高一地理教学设计:走进敦煌风成地貌的世界
- 七年级美术《拼接流动的美景》教学设计
- 小学三年级英语Unit 4 This is my friend第一课时教学设计
- 2026年烟花爆竹经营单位主要负责人考试题库及答案
- 复盘的讲解课件
- 外协加工工艺统一规范细则
- 超声造影剂安全使用专家共识(2026版)
- 太原铁路面试题目及答案
- DB53-T 1137-2023 公路建设项目电子文件与电子档案管理系统建设指南
- 股骨颈骨折患者护理 2
- TCECS-628-2019-石材粘贴工程技术规程
- 2025年人教版中考英语复习:2022年版课标核心词汇1600词 英汉+汉英 默写练习(含答案)
- 23G409先张法预应力混凝土管桩
- 2024年甘肃省职业院校技能大赛(高职学生组)财经商贸类互联网+国际经济与贸易赛项样卷3
评论
0/150
提交评论