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编辑用户成功" })); }