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

相关推荐

  • 企业怎么运营公众号,怎么策划微信集赞活动?「建议收藏」

    企业怎么运营公众号,怎么策划微信集赞活动?「建议收藏」做微信公众号运营,微信活动是微信运营的一个重要内容,吸粉、扩散知名度、发起话题、维护粉丝等运营内容都可以通过微信活动来实现。今天谈谈如何通过微信集赞活动来吸粉。首先确定好活动的主题,如xx店庆、xx开业、xx节日等等其次要提前设置好活动礼品,如满xx赞奖励xx奖品最好可以跟本行业相关,如影楼行业可设置“用户集满100赞获得3张精修照片”;医美行业“用户集满XX赞免费获得一剂瘦脸针”等。奖励条…

    2022年9月3日
  • 前端开发技术(vscode怎么下载)

    前言   在前端开发中,有一个非常好用的工具,VisualStudioCode,简称VScode。   都不用我安利VScode,大家就会乖乖的去用,无数个大言不惭的攻城狮,都被VScode比德芙还丝滑的强大功能所折服。   我是来给大家安利插件的,想做个比较全面的插件集合给大家。网上的我也看过一些,但是都比较零散,时间也久了一些,我结合最近的情况,总结一下,造福大家,才是我…

    2022年4月10日
  • 三星Samsung 4.4.2该负责人制度,简化名单「建议收藏」

    三星Samsung 4.4.2该负责人制度,简化名单

    2022年1月17日
  • JAVA校验JSON数据格式「建议收藏」

    JAVA校验JSON数据格式「建议收藏」在此博文基础上增添了更多校验功能https://blog.csdn.net/weixin_42540829/article/details/88326880publicstaticfinalclassRouteTableJsonValidator{/***数组指针*/privatestaticintindex;/***字符串*/

    2022年11月3日
  • kalidirsearch_kali.org

    kalidirsearch_kali.orgdirsearch是一个python3开发的目录扫描工具,目的是扫描网站的敏感文件和目录从而找到突破口。dirsearch使用:-u指定网址-e指定网站语言-w指定字典-r递归目录(跑出目录后,继续跑目录下面的目录)-random-agents使用随机UApythondirsearch.py-u网址-e*模板:pythondirsearch.py-uhttp://5073135b-4e2c-4551-b64f-242d901d8076.chall

    2022年9月27日
  • navicat 15 linux 在线激活码【注册码】

    navicat 15 linux 在线激活码【注册码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月20日

发表回复

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

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