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

相关推荐

  • 如何新建vue项目(如何配置vue的开发环境)

    一、vue开发环境搭建1、安装nodejs步骤:在node官网(https://nodejs.org/en/download/)选择跟自己的电脑匹配的版本进行下载,然后一步步的安装即可,在cmd控制台输入node-v,如果出现版本信息即表示安装成功。2、npm包管理器是集成在node中的,所以直接输入npm-v就能查看到版本信息,若出现版本信息则表示npm能正常使用。3、输入npminstall-gcnpm–registry=http://registry.npm.taobao.o

    2022年4月15日
  • PyQuery用法详解

    PyQuery用法详解PyQuery是强大而又灵活的网页解析库,如果你觉得正则写起来太麻烦,如果你觉得BeautifulSoup语法太难记,如果你熟悉jQuery的语法那么,PyQuery就是你绝佳的选择。一、初始化方式,有三种,可以传入字符串,传入url,传入文件。字符串初始化html=”’&lt;div&gt;&lt;ul&gt;&lt;liclass="item-…

    2022年4月30日
  • HP P2055d激光打印机PCL XL error的解决

    HP P2055d激光打印机PCL XL error的解决

    2021年11月29日
  • dede从www跟目录迁移,网站空间

    dede从www跟目录迁移,网站空间

    2021年9月20日
  • java protostuff 好处_Protostuff详解

    java protostuff 好处_Protostuff详解一、Protostuff介绍Protostuff是一个开源的、基于Java语言的序列化库,它内建支持向前向后兼容(模式演进)和验证功能。Protostuff支持的序列化格式包括:protobufprotostuffjsonsmile即二进制json,从protostuff-json模块中使用。Smile数据格式是由JacksonJSON库开发团队于2010年发布的数据格式,并在Jackson1…

    2022年6月7日
  • 用代码制作一个爱心表白_简单的代码表白跳动的爱心

    用代码制作一个爱心表白_简单的代码表白跳动的爱心/*爱心代码:*/#include<stdio.h>#include<math.h>#include<windows.h>#include<time.h>#defineU0.1#defineV0.053voidSetColor(unsignedshortForeColor,unsignedshortBackGroundColor){HANDLEhCon=GetStdHandle(STD_OUTPUT_HANDL…

    2022年8月31日

发表回复

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

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