C# 操作AD域,新建用户、组织、组

这篇博客分享了一段C#代码,用于从SHR系统同步用户、组织和组的数据。主要功能包括获取DirectoryEntry对象、用户、组织、组的查询、创建、更新以及用户列表的获取。代码实现了AD账户的管理,如禁用、启用账户,设置密码,以及将用户添加到组等操作。
摘要由CSDN通过智能技术生成

     前一段时间做了一个从SHR系统 同步用户、组织的小程序,现在分享给大家;

 废话不多说,直接上代码。

     首先引用:using System.DirectoryServices;

    定义基本连接属性:

  private string RootPath = "OU=TestOU,DC=ittest,DC=com"; //根路径
  private string ADPath = "LDAP://10.10.9.230/" ; //主机地址
  private string ADUser = "sunlizhen"; //登录账户
        //AD管理员密码
  private  string ADPasssWord = "abc123";//密码

  获取DirectoryEntry 对象

 private  DirectoryEntry GetDirectoryObject(string path ="")
        {
             //path LDAP://10.10.9.230/OU=TestOU,DC=ittest,DC=com
            DirectoryEntry entry = null;
            try
            { if (path == "")
                {
                    entry = new DirectoryEntry(ADPath + RootPath, ADUser, ADPasssWord, AuthenticationTypes.Secure);
                }
                else
                {
                    entry = new DirectoryEntry(path, ADUser, ADPasssWord, AuthenticationTypes.Secure);
                    string newguid = entry.Guid.ToString();

                }
            }
            catch (Exception ex)
            {
                entry = null;
            }
            return entry;
        }

获取用户对象

/// <summary>
        /// 根据用户公共名称取得用户的 对象
        /// </summary>
        /// <param name="commonName">用户公共名称</param>
        /// <returns>如果找到该用户则返回用户的对象,否则返回 null</returns>
        public  DirectoryEntry GetUserEntry(string commonName)
        {
            DirectoryEntry de = GetDirectoryObject();
            DirectorySearcher deSearch = new DirectorySearcher(de);
            deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user))(cn=" + commonName.Replace("\\", "") + "))";
            deSearch.SearchScope = SearchScope.Subtree;
            try
            {
                SearchResult result = deSearch.FindOne();
                if (result == null)
                    return null;
                //de = new DirectoryEntry(result.Path);
                de = GetDirectoryObject(result.Path);
                return de;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

获取组织对象

/// <summary>
        // 获取组织单位
        /// </summary>
     
        /// <param name="ouname">组织名称</param>
        /// <returns></returns>
        public DirectoryEntry GetOU(string ouname)
        {
           
            DirectorySearcher deSearch = new DirectorySearcher();
            deSearch.Filter = string.Format("(&(objectClass=organizationalUnit) (OU={0}))", ouname);
            SearchResult results = deSearch.FindOne();
            if (results != null)
            {
                return results.GetDirectoryEntry();
            }
            else
            {
                return null;
            }
        }

获取group对象

/// <summary>
        /// 根据组名获取组织对象
        /// </summary>
        /// <param name="commonName">组名</param>
        /// <returns>如果找到该用户则返回用户的对象,否则返回 null</returns>
        public DirectoryEntry GetGroupEntry(string commonName)
        {
            DirectoryEntry de = GetDirectoryObject();
            DirectorySearcher deSearch = new DirectorySearcher(de);
            deSearch.Filter = "(&(objectClass=group)(cn=" + commonName + "))";
            deSearch.SearchScope = SearchScope.Subtree;
            try
            {
                SearchResult result = deSearch.FindOne();
                if (result == null)
                    return null;
                //  de = new DirectoryEntry(result.Path);
                de = GetDirectoryObject(result.Path);
                return de;
            }
            c
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值