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


相关推荐

  • 【NOIP2012提高组】借教室[通俗易懂]

    【NOIP2012提高组】借教室[通俗易懂]题目背景NOIP2012 提高组 DAY2 试题。题目描述在大学期间,经常需要租借教室。大到院系举办活动,小到学习小组自习讨论,都需要向学校申请借教室。教室的大小功能不同,借教室人的身份不同,借教室的手续也不一样。面对海量租借教室的信息,我们自然希望编程解决这个问题。我们需要处理接下来 n 天的借教室信息,其中第 i 天学校有 ri 个教室可供租借。共有 m 份订单,每份订单用三个…

    2022年8月22日
    5
  • nsga2 matlab,NSGA2算法特征选择MATLAB实现(多目标)

    nsga2 matlab,NSGA2算法特征选择MATLAB实现(多目标)利用nsga2进行进行特征选择其主要思想是:将子集的选择看作是一个搜索寻优问题(wrapper方法),生成不同的组合,对组合进行评价,再与其他的组合进行比较。这样就将子集的选择看作是一个是一个优化问题。需要优化的两个目标为特征数和精度。nsga2是一个多目标优化算法。具体的特征选择代码在上述代码的基础上改了两个①主函数②评价函数,增加了一个数据分成训练集和测试集的函数:MATLABfunction…

    2022年5月19日
    37
  • CentOS 7 x8安装记录

    CentOS 7 x8安装记录CentOS7x8安装插入刻录好的U盘(刻录这里我就不说了,有实用的刻录链接可以下方留言给我我补充上)按下拥有弹性的开机按钮,开启你的装机旅程,启动时注意!按F11进入BootMenu一、在BootMenu中选择UEFI:VendorCoProductCode,Partition1如果选择了VendorCoProductCode安装结束后,启动系统后就会下面这副惨样子…

    2022年5月25日
    79
  • AlertDialog.Builder中的setMultiChoiceItems中的事件处理[通俗易懂]

    AlertDialog.Builder中的setMultiChoiceItems中的事件处理

    2022年1月28日
    37
  • 什么叫侧面指纹识别_正面背面侧面 你手机的指纹识别长在哪?

    什么叫侧面指纹识别_正面背面侧面 你手机的指纹识别长在哪?指纹识别已成为手机行业对热门技术之一,目前市面上常见的指纹识别模块搭载的位置主要有,正面、后背、侧面三种,比如说三星GalaxyS6、OPPOR9集成在正面Home键里,小米Note3、华为Mate8等装在了手机背部,LGV10植入到手机后边的电源键里,索尼XperiaZ5则放在手机侧面。正面背面侧面你手机的指纹识别长在哪?有人说指纹识别在正面最科学,但有些人则认为在背面更合理,此外…

    2022年6月23日
    64
  • 图解Golang的GC算法「建议收藏」

    图解Golang的GC算法「建议收藏」虽然Golang的GC自打一开始,就被人所诟病,但是经过这么多年的发展,Golang的GC已经改善了非常多,变得非常优秀了。以下是GolangGC算法的里程碑:v1.1STWv1.3MarkSTW,Sweep并行v1.5三色标记法v1.8hybridwritebarrier经典的GC算法有三种:引用计数(referencecounting)、标记-清扫(mar…

    2022年6月22日
    51

发表回复

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

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