/// <summary> /// 添加新组到指定的组织单元 /// </summary> /// <param name="cn">用户组</param> /// <param name="ouPath">组织单元路径(格式:OU=sddsd,OU=sdsdsd,顺序,子倒父)</param> /// <param name="description">描述</param> /// <returns>bool</returns> public int CreateGroupToUnit(string cn, string description, string path, out string errStr) { int result = 0; errStr = ""; //创建指定路径的组织单元对象 int i = 0; int j = 0; //string LDAPDomain ="/"+ouPath.ToString()+Iadc.GetLDAPDomain() ; //string LDAPDomain ="/"+ouPath.ToString()+ Iadc.GetLDAPDomain() ; //DirectoryEntry oDE= Iadc.GetDirectoryObject(LDAPDomain); DirectoryEntry oDE = Iadc.GetDirectoryObject(Iads.GetUnit(cn).ToString(), i, j); //DirectoryEntry oDE= Iadc.GetDirectoryObject(ouPath); DirectoryEntry oDEC = new DirectoryEntry(); try { if (!Iadch.CheckGroup(cn)) { oDEC = oDE.Children.Add("cn=" + cn, "group"); //oDEC.Properties["grouptype"].Value = ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_GLOBAL_GROUP | ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_SECURITY_ENABLED ; oDEC.Properties["sAMAccountName"].Value = cn; oDEC.Properties["description"].Value = description; oDEC.Properties["displayName"].Value = path; oDEC.CommitChanges(); result = 1; } else { //移动组到正确的OU中 oDEC = Iads.GetGroupEntry(cn); oDEC.Properties["displayName"].Value = path; oDEC.CommitChanges(); oDEC.MoveTo(oDE); oDE.CommitChanges(); result = 2; errStr = "目录已存在该组,不能重复添加"; } } catch (Exception err) { result = 0; errStr = err.ToString(); } finally { oDE.Close(); oDEC.Close(); } return(result); }
// /// <summary> // /// 创建新账号到指定的组下 // /// </summary> // /// <param name="cn"></param> // /// <returns></returns> public bool CreateNewUserToGroup(string cn, string name, string group, out string errStr) // modi zl 4.26 { bool result = false; DirectoryEntry AD = new DirectoryEntry("LDAP://" + "DC=sinomaster.com,DC=zxkj"); DirectoryEntry NewUser = AD.Children.Add("TestUser1", "user"); //帐号 NewUser.Invoke("SetPassword", new object[] { "#12345Abc" }); // 密码 NewUser.Invoke("Put", new object[] { "Description", "Test User from .NET" }); NewUser.CommitChanges(); DirectoryEntry grp; grp = AD.Children.Find("Guests", "group"); if (grp != null) { grp.Invoke("Add", new object[] { NewUser.Path.ToString() }); } //////////////////// //DirectoryEntry AD = Iads.GetGroupEntry(group); //DirectoryEntry NewUser = AD.Children.Add("TestUser1", "user"); //帐号 //NewUser.Invoke("SetPassword", new object[] { "1234" }); // 密码 //NewUser.Invoke("Put", new object[] { "Description", "Test User from .NET" }); //NewUser.CommitChanges(); DirectoryEntry oDE = Iads.GetGroupEntry(group); errStr = ""; DirectoryEntry oDEC = new DirectoryEntry(); try { if (!Iadch.CheckUser(cn)) { oDEC = oDE.Children.Add("CN=" + cn, "user"); oDEC.Properties["sAMAccountName"].Value = cn; oDEC.Properties["description"].Value = name; oDEC.Properties["userAccountControl"].Value = ActiveDs.ADS_USER_FLAG.ADS_UF_NORMAL_ACCOUNT; oDEC.CommitChanges(); result = true; } else { errStr = "目录下已存在同名的账号"; result = false; } } catch (Exception err) { errStr = err.ToString(); result = false; } finally { oDE.Close(); } return(result); }