public ActionResult AddOUDept(string parentDeptName, string deptName)
        {
            string         filePath = Server.MapPath("~/ADConfig.xml");
            AdOperate      ado      = new AdOperate(filePath);
            DirectoryEntry entry    = ado.GetEntry();
            string         filter   = "(&(objectclass=organizationalUnit)(ou=" + parentDeptName + "))";
            DirectoryEntry ouEntry  = ado.GetOUEntry(entry, filter);

            if (!ado.AddOUEntry(ouEntry, deptName))
            {
                return(Json(new AjaxResult {
                    Status = "error", Msg = "ad域中添加部门失败"
                }));
            }
            RtxDeptManager rdm = new RtxDeptManager();

            if (!rdm.AddDept(deptName, parentDeptName))
            {
                filter = "(&(objectclass=organizationalUnit)(ou=" + deptName + "))";
                ado.DelEntry(ado.GetOUEntry(entry, filter));
                return(Json(new AjaxResult {
                    Status = "error", Msg = "RTX中添加部门失败"
                }));
            }
            return(Json(new AjaxResult {
                Status = "ok", Msg = "部门同步添加成功"
            }));
        }
        public ActionResult EditOUDept(string deptName, string newDeptName)
        {
            string         filePath = Server.MapPath("~/ADConfig.xml");
            AdOperate      ado      = new AdOperate(filePath);
            DirectoryEntry entry    = ado.GetEntry();
            string         filter   = "(&(objectclass=organizationalUnit)(ou=" + deptName + "))";
            DirectoryEntry ouEntry  = ado.GetOUEntry(entry, filter);

            if (!ado.OUEntryReName(ouEntry, newDeptName))
            {
                return(Json(new AjaxResult {
                    Status = "error", Msg = "ad域中编辑部门失败"
                }));
            }
            RtxDeptManager rdm = new RtxDeptManager();

            if (!rdm.SetDeptName(deptName, newDeptName))
            {
                filter = "(&(objectclass=organizationalUnit)(ou=" + newDeptName + "))";
                ado.OUEntryReName(ado.GetOUEntry(entry, filter), deptName);
                return(Json(new AjaxResult {
                    Status = "error", Msg = "RTX中编辑部门失败"
                }));
            }
            return(Json(new AjaxResult {
                Status = "ok", Msg = "部门同步编辑成功"
            }));
        }
        public ActionResult AddUser(DomainUser user)
        {
            string filePath = Server.MapPath("~/ADConfig.xml");
            //AdOperate ado = new AdOperate();
            //ado.GetADConfig(filePath, user.ComName);
            AdOperate ado = new AdOperate(filePath);
            //string domainPath = "LDAP://192.168.31.134/OU=南宁公司,DC=test,DC=com";
            //string adminUser = "******";
            //string password = "******";
            //DirectoryEntry entry= ado.GetEntry(domainPath, adminUser,password);
            DirectoryEntry entry   = ado.GetEntry();
            string         filter  = "(&(objectclass=organizationalUnit)(ou=" + user.Department + "))";
            DirectoryEntry ouEntry = ado.GetOUEntry(entry, filter);

            if (ouEntry == null)
            {
                return(Json(new AjaxResult {
                    Status = "error", Msg = "nonDept"
                }));
            }
            JavaScriptSerializer js = new JavaScriptSerializer();
            string data             = js.Serialize(user);

            if (ado.IsADUserExist(ouEntry, user.Name))
            {
                return(Json(new AjaxResult {
                    Status = "error", Msg = "用户已经存在"
                }));
            }
            //return Json(new AjaxResult { Status = "error", Msg = "userdata", Data = data });
            if (!ado.AddAccount(ouEntry, user))
            {
                return(Json(new AjaxResult {
                    Status = "error", Msg = "添加用户到域失败", Data = data
                }));
            }
            RtxManager rm = new RtxManager();

            string[] paths = ouEntry.Path.Replace("LDAP://192.168.31.134/", "").Replace(",DC=test,DC=com", "").Replace("OU=", "").Split(',');
            string   path  = "";

            for (int i = paths.Length - 1; i >= 0; i--)
            {
                path = path + paths[i] + @"\";
            }

            if (!rm.AddEditRtxUser(user, path, 1))
            {
                ado.GetUserEntry(entry, user.Name).DeleteTree();
                return(Json(new AjaxResult {
                    Status = "error", Msg = "rtx添加用户失败"
                }));
            }
            return(Json(new AjaxResult {
                Status = "ok", Msg = "success", Data = path
            }));
        }
        public ActionResult EditUser(DomainUser user)
        {
            string    filePath = Server.MapPath("~/ADConfig.xml");
            AdOperate ado      = new AdOperate(filePath);
            //ado.SetADConfig(user.ComName, filePath);
            DirectoryEntry entry   = ado.GetEntry();
            string         filter  = "(&(objectclass=organizationalUnit)(ou=" + user.Department + "))";
            DirectoryEntry ouEntry = ado.GetOUEntry(entry, filter);

            if (ouEntry == null)
            {
                return(Json(new AjaxResult {
                    Status = "error", Msg = "部门不存在"
                }));
            }
            DirectoryEntry userEntry = ado.GetUserEntry(entry, user.Name);

            if (userEntry == null)
            {
                return(Json(new AjaxResult {
                    Status = "error", Msg = "用户不存在"
                }));
            }
            if (!userEntry.Path.Contains(user.Department))
            {
                ado.UserMoveToOU(ouEntry, userEntry);
            }
            if (!ado.EditAccount(userEntry, user))
            {
                return(Json(new AjaxResult {
                    Status = "error", Msg = "编辑用户到域失败"
                }));
            }
            RtxManager rm = new RtxManager();

            string[] paths = ouEntry.Path.Replace("LDAP://192.168.31.134/", "").Replace(",DC=test,DC=com", "").Replace("OU=", "").Split(',');
            string   path  = "";

            for (int i = paths.Length - 1; i >= 0; i--)
            {
                path = path + paths[i] + @"\";
            }

            if (!rm.AddEditRtxUser(user, path, 1))
            {
                ado.GetUserEntry(entry, user.Name).DeleteTree();
                return(Json(new AjaxResult {
                    Status = "error", Msg = "rtx编辑用户失败"
                }));
            }
            return(Json(new AjaxResult {
                Status = "ok", Msg = "rtx编辑用户成功"
            }));
        }
        public ActionResult AddOU(DomainUser user)
        {
            string         filePath = Server.MapPath("~/ADConfig.xml");
            AdOperate      ado      = new AdOperate(filePath);
            DirectoryEntry entry    = ado.GetEntry();
            string         filter   = "(&(objectclass=organizationalUnit)(ou=行政部))";
            DirectoryEntry ouEntry  = ado.GetOUEntry(entry, filter);

            //DirectoryEntry userEntry = ado.GetUserEntry(entry,"yilidan");
            //JavaScriptSerializer js = new JavaScriptSerializer();
            return(Json(new AjaxResult {
                Status = "error", Msg = "想不通", Data = entry.Path + ":::" + ouEntry.Path + ":::" + user.Name
            }));
        }
        public ActionResult DelUser(string userName)
        {
            string         filePath  = Server.MapPath("~/ADConfig.xml");
            AdOperate      ado       = new AdOperate(filePath);
            DirectoryEntry entry     = ado.GetEntry();
            DirectoryEntry userEntry = ado.GetUserEntry(entry, userName);

            ado.DelEntry(userEntry);
            RtxUserManager rum = new RtxUserManager();

            rum.RemoveUser(userName);
            return(Json(new AjaxResult {
                Status = "ok", Msg = "用户删除成功"
            }));
        }
        public ActionResult DelOUDept(string deptName)
        {
            string         filePath = Server.MapPath("~/ADConfig.xml");
            AdOperate      ado      = new AdOperate(filePath);
            DirectoryEntry entry    = ado.GetEntry();
            string         filter   = "(&(objectclass=organizationalUnit)(ou=" + deptName + "))";
            DirectoryEntry ouEntry  = ado.GetOUEntry(entry, filter);

            ado.DelEntry(ouEntry);
            RtxDeptManager rdm = new RtxDeptManager();

            rdm.DelDept(deptName);
            return(Json(new AjaxResult {
                Status = "ok", Msg = "部门删除成功"
            }));
        }
Beispiel #8
0
        public ActionResult SyncRtx(string comName)
        {
            string    filePath = Server.MapPath("~/ADConfig.xml");
            AdOperate ado      = new AdOperate(filePath);
            //ado.GetADConfig(filePath,comName);
            DirectoryEntry entry  = ado.GetEntry();
            RtxDeptManager dept   = new RtxDeptManager();
            RtxUserManager user   = new RtxUserManager();
            string         filter = "objectclass=organizationalUnit";

            ado.OUEntrySyncRtx(entry, filter, dept);
            filter = "(&(objectCategory=person)(objectClass=user))";
            ado.UserEntrySyncRtx(entry, filter, user, dept);
            return(Json(new AjaxResult {
                Status = "ok", Msg = "success"
            }));
        }
Beispiel #9
0
        static void Main6(string[] args)
        {
            string         filePath = @" F:\1708\RtxLdap\TestCode\ADConfig.xml";
            AdOperate      ado      = new AdOperate(filePath);
            DirectoryEntry entry    = ado.GetEntry();
            RtxDeptManager dept     = new RtxDeptManager();
            RtxUserManager user     = new RtxUserManager();
            string         filter   = "objectclass=organizationalUnit";

            ado.OUEntrySyncRtx(entry, filter, dept);
            filter = "(&(objectCategory=person)(objectClass=user))";
            ado.UserEntrySyncRtx(entry, filter, user, dept);
            //if(ado.CheckADUser(path,"leinuo","Asd654123"))
            //{
            //    Console.WriteLine("测试成功");
            //}
            Console.WriteLine("测试成功");
            Console.ReadKey();
        }
Beispiel #10
0
        public ActionResult EditOU()
        {
            string         filePath = Server.MapPath("~/ADConfig.xml");
            AdOperate      ado      = new AdOperate(filePath);
            DirectoryEntry entry    = ado.GetEntry("LDAP://192.168.31.134/OU=南宁公司,DC=test,DC=com", "Administrator", "Abc123456");
            DirectoryEntry ouEntry  = ado.GetOUEntry(entry, "(&(objectclass=organizationalUnit)(ou=php开发))");
            DomainUser     user     = new DomainUser();

            user.Name            = "anna";
            user.DisplayName     = "安娜";
            user.TelephoneNumber = "18618618686";
            user.UserPwd         = "Asd123456";
            user.Mail            = "*****@*****.**";

            try
            {
                DirectoryEntry NewUser = ouEntry.Children.Add("CN=" + user.Name, "user");
                NewUser.Properties["sAMAccountName"].Add(user.Name);                     //account
                NewUser.Properties["userPrincipalName"].Value = user.Name + "@test.com"; //user logon name,[email protected]
                if (!string.IsNullOrEmpty(user.Company))
                {
                    NewUser.Properties["company"].Value = user.Company;
                }
                if (!string.IsNullOrEmpty(user.Department))
                {
                    NewUser.Properties["department"].Value = user.Department;
                }
                if (!string.IsNullOrEmpty(user.Description))
                {
                    NewUser.Properties["description"].Value = user.Description;
                }
                if (!string.IsNullOrEmpty(user.DisplayName))
                {
                    NewUser.Properties["displayName"].Value = user.DisplayName;
                }
                if (!string.IsNullOrEmpty(user.GivenName))
                {
                    NewUser.Properties["givenName"].Value = user.GivenName;
                }
                if (!string.IsNullOrEmpty(user.Initials))
                {
                    NewUser.Properties["initials"].Value = user.Initials;
                }
                if (!string.IsNullOrEmpty(user.Mail))
                {
                    NewUser.Properties["mail"].Value = user.Mail;
                }
                if (!string.IsNullOrEmpty(user.Name))
                {
                    NewUser.Properties["name"].Value = user.Name;
                }
                if (!string.IsNullOrEmpty(user.PhysicalDeliveryOfficeName))
                {
                    NewUser.Properties["physicalDeliveryOfficeName"].Value = user.PhysicalDeliveryOfficeName;
                }
                if (!string.IsNullOrEmpty(user.SN))
                {
                    NewUser.Properties["sn"].Value = user.SN;
                }
                if (!string.IsNullOrEmpty(user.TelephoneNumber))
                {
                    NewUser.Properties["telephoneNumber"].Value = user.TelephoneNumber;
                }
                NewUser.Properties["initials"].Value = user.Gender;
                NewUser.CommitChanges();
                //设置密码
                //反射调用修改密码的方法(注意端口号的问题  端口号会引起方法调用异常)

                NewUser.Invoke("SetPassword", new object[] { user.UserPwd });
                NewUser.Properties["userAccountControl"].Value = 0x200;
                //默认设置新增账户启用
                NewUser.CommitChanges();
                return(Json(new AjaxResult {
                    Status = "ok", Msg = "添加成功"
                }));
            }
            catch (Exception ex)
            {
                return(Json(new AjaxResult {
                    Status = "error", Msg = "添加失败" + ex.ToString()
                }));
            }
        }