c# 操作ad域用户

c# 操作ad域用户测试环境:win2008r2服务器ad域服务器安装参考:https://www.cnblogs.com/cnjavahome/p/9029665.html密码策略修改参考:https://blog.csdn.net/zouyujie1127/article/details/40857675工作机dns设置为ad域服务器的ipusing:usingSystem.DirectoryServ…

大家好,又见面了,我是你们的朋友全栈君。

测试环境:win2008r2服务器

ad域服务器安装参考:https://www.cnblogs.com/cnjavahome/p/9029665.html
密码策略修改参考:https://blog.csdn.net/zouyujie1127/article/details/40857675

工作机dns设置为ad域服务器的ip

using:

using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;

获取组织单位:

 public DirectoryEntry GetOU(DirectoryEntry parent, string ouname)
        {
            DirectorySearcher mySearcher = new DirectorySearcher(parent, "(objectclass=organizationalUnit)");
            DirectorySearcher deSearch = new DirectorySearcher();
            deSearch.SearchRoot = parent;
            deSearch.Filter = string.Format("(&(objectClass=organizationalUnit) (OU={0}))", ouname);
            SearchResult results = deSearch.FindOne();
            if (results != null)
            {
                return results.GetDirectoryEntry();
            }
            else
            {
                return null;
            }
        }

建组织单位:

public void AddOU(DirectoryEntry parent, string ouname)
        {
            DirectoryEntries ous = parent.Children;
            DirectoryEntry ou = ous.Add("OU=" + ouname, "organizationalUnit");
            ou.CommitChanges();
            ou.Close();
        }

建立连接:

 public PrincipalContext createConnection(List<string> oupath = null)
        {
            string path = "";
            foreach (string str in _domainArr)
            {
                path += string.Format(",DC={0}", str);
            }
            if (oupath != null)
            {
                string tmp = "";
                for (int i = oupath.Count - 1; i >= 0; i--)
                {
                    tmp += string.Format(",OU={0}", oupath[i]);
                }
                tmp = tmp.Substring(1);
                path = tmp + path;
            }
            else
            {
                path = path.Substring(1);
            }

            var context = new PrincipalContext(ContextType.Domain, _domain, path, ContextOptions.Negotiate, _adminName, _adminPass);
            return context;
        }

建用户:

public void AddUser(PrincipalContext context, string barcode, string userName, string passWord)
        {
            using (UserPrincipal u = new UserPrincipal(context, barcode, passWord, true))
            {
                u.Name = barcode;
                u.DisplayName = userName;
                u.UserCannotChangePassword = true;
                u.PasswordNotRequired = true;
                u.PasswordNeverExpires = true;
                u.UserPrincipalName = barcode + "@" + _domain;
                u.Save();
            }
        }

修改密码:

public void EditPass(string userName, string passWord)
        {
            using (var context = createConnection())
            {
                UserPrincipal user = UserPrincipal.FindByIdentity(context, userName);
                if (user != null)
                {
                    user.SetPassword(passWord);
                    user.Save();
                }
            }
        }

删除用户:

public void DelUser(string userName)
        {
            using (var context = createConnection())
            {
                UserPrincipal user = UserPrincipal.FindByIdentity(context, userName);
                if (user != null)
                {
                    user.Delete();
                }
            }
        }

登录验证:

  public bool login(string name, string password)
        {
            DirectoryEntry root = null;
            try
            {
                string ADPath = rootPath();
                root = new DirectoryEntry(ADPath, name, password, AuthenticationTypes.Secure);
                string strName = root.Name;
                root.Close();
                root = null;
                return true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                return false;
            }
        }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/145648.html原文链接:https://javaforall.net

(1)
上一篇 2022年5月16日 下午10:00
下一篇 2022年5月16日 下午10:00


相关推荐

  • mysql 唯一索引_mysql主键和唯一索引的区别

    mysql 唯一索引_mysql主键和唯一索引的区别Mysql索引大概有五种类型:普通索引(INDEX):最基本的索引,没有任何限制唯一索引(UNIQUE):与”普通索引”类似,不同的就是:索引列的值必须唯一,但允许有空值。主键索引(PRIMARY):它是一种特殊的唯一索引,不允许有空值。全文索引(FULLTEXT):可用于MyISAM表,mysql5.6之后也可用于innodb表,用于在一篇文章中,检索文本信息的,针对较大的数据,生成全文索引很耗时和空间。联合(组合)索引:为了更多的提高mysql效率可建立组合索引,遵循”最左前缀“原

    2026年1月31日
    3
  • Git可视化教程——Git Gui的使用[通俗易懂]

    Git可视化教程——GitGui的使用

    2022年4月12日
    1.0K
  • onResume无限循环

    onResume无限循环今天在做权限申请,写完后,发现点界面上任何东西都无法响应,整个界面处于卡死状态。查看Log,onResume和onPause在不停的执行,debug排查发现是因为请求权限导致的。由于考虑到权限必须获得,否则没法去读取数据和开启功能,所以就想着放在onResume里。请求权限相当于打开了一个请求权限界面,app会先执行onPause,如果有多个权限申请,点了允许,生命周期会执

    2022年6月2日
    33
  • No pyvenv.cfg file问题解决方案

    No pyvenv.cfg file问题解决方案nopyvenv cfgfile 问题解决方案百度了一下发现百度也不是万能的解决方法 file 里面的 setting 点击小齿轮再点击 Add 在里面添加路径就可以了路径就是在之前的文件位置附近找总会找到的我就是无脑随便点点找到了找文件错误的话会提醒你但是如果你完成上述步骤的话 你会发现你下载的第三方库都没了 有个好办法就是用 Andconda 的解释器 如何使用呢 就是修改 baseInterpre 就行了之后就可以运行了总体来看 这篇博客写的挺烂的 有问题可以告诉

    2026年3月19日
    2
  • python中绝对值的表达式

    python中绝对值的表达式abs(x)print(abs(23))#23print(abs(-45))#45

    2022年7月6日
    21
  • OpenClaw v2026.3.12 离线源码构建与 Docker 部署完整教程

    OpenClaw v2026.3.12 离线源码构建与 Docker 部署完整教程

    2026年3月14日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号