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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Java和Python哪个更好?

    Java和Python哪个更好?一些开发人员声称Python比Java更有效率。但这应该先弄清Python和Java之间的区别是什么?Java和Python的区别Java是一种严格的类型语言,这意味着必须显式声明变量名。相比之下,动态类型的Python则不需要声明变量。在编程语言上有许多关于动态和静态类型的争论,但有一点应该注意:Python是一种语法简单的功能强大的语言,能够通过编写脚本就提供优秀的解决方案,并能够快捷…

    2022年7月8日
    18
  • polkitd进程解释

    polkitd进程解释今天想kill-9redis杀不掉然后发现这个是属于服务器方法,可以了解一下supervisor,将需要自启动的程序加入到supervisor的启动配置,只要supervisor不停止,那么监控进程就会一直运行,并且如果出现关闭情况也会被立即重启。…

    2022年6月14日
    114
  • [黑苹果系列] M910x完美黑苹果系统安装教程 – 2 制作系统U盘-USB Creation

    [黑苹果系列] M910x完美黑苹果系统安装教程 – 2 制作系统U盘-USB Creation目前主流的苹果系统的安装方法有两种:1.U盘安装2.Windows下使用镜像恢复软件安装的方式目前,U盘安装是主流选择,这样安装调试好的黑苹果macOS问题最少,也较为稳定。之前的文章由于采用的是镜像恢复的方法,此次安装采取U盘安装系统的方式,因此重新写一篇。1.前期准备U盘一个,大于16G 安装工具软件包 镜像文件 制作合适的EFI文件 需要准备40GB以上独立固态硬盘空间2.下载macOs镜像3.制作安装U盘可以用TransMac或者balenaEt.

    2022年6月9日
    96
  • COM编程之三 QueryInterface

    COM编程之三 QueryInterface【1】IUnknown接口客户同组件交互都是通过接口完成的。在客户查询组件的其它接口时,也是通过接口完成的。而那个接口就是IUnknown。IUnknown接口的定义包含在Win32SDK中的U

    2022年7月4日
    24
  • SSIS 包配置

    SSIS 包配置

    2022年3月2日
    40
  • 我入职阿里后,才知道原来简历这么写

    我入职阿里后,才知道原来简历这么写私下里,有不少读者问我:“二哥,如何才能写出一份专业的技术简历呢?我总感觉自己写的简历太烂了,所以投了无数份,都石沉大海了。”说实话,我自己好多年没有写过简历了,但我认识的一个同行,他在阿里,给我说了一些他当年写简历的方法论,我感觉太牛逼了,实在是忍不住,就分享了出来,希望能够帮助到你。01、简历的本质作为简历的撰写者,你必须要搞清楚一点,简历的本质是什么,它就是为了来销售你的价值主张的。往深…

    2022年5月22日
    34

发表回复

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

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