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


相关推荐

  • 2019/7/3

    2019/7/31176E-Coverit!树的题常与层数,出度有关

    2022年6月16日
    40
  • Springboot学习笔记【持续更新中】

    Springboot学习笔记【持续更新中】

    2021年7月12日
    83
  • 数据库连接池学习笔记(一):原理介绍+常用连接池介绍

    数据库连接池学习笔记(一):原理介绍+常用连接池介绍什么是连接池数据库连接池负责分配、管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而不是再重新建立一个。为什么要使用连接池 数据库连接是一种关键的有限的昂贵的资源,这一点在多用户的网页应用程序中体现得尤为突出。 一个数据库连接对象均对应一个物理数据库连接,每次操作都打开一个物理连接,使用完都关闭连接,这样造成系统的性能低下。数据库连接池的解决方案是在应用程序启动…

    2022年9月17日
    2
  • Oracle查看那些用户具有DBA权限「建议收藏」

    Oracle查看那些用户具有DBA权限「建议收藏」Oracle查看哪些用户具有DBA权限colGRANTEEfora20colgranted_rolefora20select*fromsys.dba_role_privswheregranted_role=’DBA’;GRANTEEGRANTED_ROLEADMDELDEFCOMINH——————————————————-AAA_DBA

    2022年9月25日
    4
  • android之SQLite数据库insert操作

    原型:long android.database.sqlite.SQLiteDatabase.insert(String table, String nullColumnHack, ContentValues values) 参数介绍:table: 要插入数据的表的名称nullColumnHack:当values参数为空或者里面没有内容的时候,我们insert是会失败的(底

    2022年3月10日
    246
  • Nginx 配置中nginx和alias的区别分析

    Nginx 配置中nginx和alias的区别分析root和alias都可以定义在location模块中,都是用来指定请求资源的真实路径,比如:?123location/i/{root/data/w3;}请求http://foofish.net/i/top.gif这个地址时,那么在服务器里面对应的真正的资源是/data/w3/i/top.gif文件注意:真实的路径是root指定的值加上location指定的值。而alias正…

    2022年7月14日
    51

发表回复

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

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