/// <summary> /// 创建域用户,"administrator","Ccc2008neu","administrator","Ccc2008neu" /// </summary> /// <param name="loginName"></param> /// <param name="displayName"></param> /// <param name="description"></param> /// <param name="pwd"></param> public static bool AddUser(string loginName, string displayName, string email, string phone, string pwd, string topPath, string groupName, string schoolName, bool enabled) { string ouPath = AddOU(topPath, schoolName); bool result; string content = ""; //先加安全组,帐号重复会出错;否则会出现错误 DirectoryEntry grp = AddGroup(new DirectoryEntry(topPath), groupName); using (DirectoryEntry AD = new DirectoryEntry(ouPath)) { try { using (DirectoryEntry NewUser = AD.Children.Add("CN=" + loginName, "user")) { NewUser.Properties["displayName"].Add(displayName); NewUser.Properties["name"].Add(displayName); NewUser.Properties["sAMAccountName"].Add(loginName); NewUser.Properties["userPrincipalName"].Add(loginName + DomainName); if (phone != "") { NewUser.Properties["telephoneNumber"].Add(phone); } if (email != "") { NewUser.Properties["mail"].Add(email); } NewUser.CommitChanges(); try { ActiveDs.IADsUser user = (ActiveDs.IADsUser)NewUser.NativeObject; user.AccountDisabled = !enabled; user.SetPassword(pwd); //密码永不过期 dynamic flag = user.Get("userAccountControl"); int newFlag = 0X10000; user.Put("userAccountControl", newFlag); user.SetInfo(); NewUser.CommitChanges(); } catch (Exception ex) { content += ex.ToString() + "\r\f"; } if (groupName != "") { AddUserToGroup(grp, NewUser); } result = true; } } catch (Exception ex) { content += ex.ToString(); result = false; } } return(result); }
//更改密码 public static bool ChangePassword(string loginName, string newPassword) { DirectoryEntry NewUser = ADHelper.GetDirectoryEntryByAccount(loginName); ActiveDs.IADsUser user = (ActiveDs.IADsUser)NewUser.NativeObject; try { user.SetPassword(newPassword); NewUser.CommitChanges(); return(true); } catch { return(false); } }
static void EnableUser() { using (DirectoryEntry de = new DirectoryEntry()) { de.Path = "LDAP://celticrain/CN=John Doe, CN=Users, DC=eichkogelstrasse, DC=local"; de.Invoke("SetPassword", "anotherSecret"); de.CommitChanges(); ActiveDs.IADsUser user = (ActiveDs.IADsUser)de.NativeObject; user.SetPassword("someSecret"); user.AccountDisabled = false; de.CommitChanges(); } }
//更改密码 public static bool ChangePassword(string loginName, string newPassword, ref string errMsg) { DirectoryEntry NewUser = ADHelper.GetDirectoryEntryByAccount(loginName); try { ActiveDs.IADsUser user = (ActiveDs.IADsUser)NewUser.NativeObject; user.SetPassword(newPassword); NewUser.CommitChanges(); return(true); } catch (Exception ex) { errMsg = ex.ToString(); return(false); } }