//编写用户的单位名称 public static bool EditUserDepartment(string userDep, string loginName) { DirectoryEntry currentUser = ADHelper.GetDirectoryEntryByAccount(loginName);//当前被编辑的用户 //currentUser.Properties["name"][0] = displayName;//执行此句会出现错误 if (userDep != "") { if (currentUser.Properties.Contains("department")) { currentUser.Properties["department"][0] = userDep; } else { currentUser.Properties["department"].Add(userDep);//家庭电话otherTelephone } try { currentUser.CommitChanges(); return(true); } catch { return(false); } } return(true); }
//用户在ad中是否存在 public static bool UserExits(string account) { DirectoryEntry adUser = ADHelper.GetDirectoryEntryByAccount(account); if (adUser != null) { return(true); } return(false); }
//更改密码 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); } }
/// <summary> /// 修改AD中的用户信息,启动帐号(帐号禁用帐号) /// </summary> /// <param name="loginName"></param> /// <returns></returns> public static bool EnabledUser(string loginName, bool userEnabled) { using (DirectoryEntry NewUser = ADHelper.GetDirectoryEntryByAccount(loginName)) { try { ActiveDs.IADsUser user = (ActiveDs.IADsUser)NewUser.NativeObject; user.AccountDisabled = !userEnabled; user.SetInfo(); NewUser.CommitChanges(); return(true); } catch { return(false); } } }
private static void FillUserInfo(ref DataRow dr, string account) { DirectoryEntry adUser = ADHelper.GetDirectoryEntryByAccount(account);//当前被编辑的用户 if (adUser != null) { dr["Name"] = adUser.Properties["displayName"][0]; //dr["Sex"] = int.Parse(rblSex.SelectedValue); if (adUser.Properties.Contains("telephoneNumber")) { dr["Telephone"] = adUser.Properties["telephoneNumber"][0]; } if (adUser.Properties.Contains("mail")) { dr["Email"] = adUser.Properties["mail"][0]; } dr["Flag"] = 1; dr["Modified"] = DateTime.Now; } }
/// <summary> /// 用户在AD中是否存在 /// </summary> /// <param name="account"></param> /// <param name="userState">返回用户的状态</param> /// <returns></returns> public static bool UserExits(string account, ref int userState) { DirectoryEntry adUser = ADHelper.GetDirectoryEntryByAccount(account); if (adUser != null) { try { ActiveDs.IADsUser user = (ActiveDs.IADsUser)adUser.NativeObject; userState = user.AccountDisabled ? 0 : 1; } catch { userState = -1; } return(true); } return(false); }
//用户登录帐号-解析后的 public static string GetUserDept(string loginName) { string dept = ""; DirectoryEntry de = ADHelper.GetDirectoryEntryByAccount(loginName); DirectorySearcher ds = new DirectorySearcher(de); ds.Filter = ("(SAMAccountName=" + loginName + ")"); SearchResult dss = ds.FindOne(); if (dss != null) { string dpath = dss.Path; dpath = dpath.Substring(dpath.IndexOf("OU=") + 3); dept = dpath.Substring(0, dpath.IndexOf(",")); } else { dept = "管理员"; } return(dept); }
/// <summary> /// 编辑AD中已经注册的当前用户信息 /// </summary> /// <param name="loginName">当前用户的登录名</param> /// <param name="displayName">当前用户的显示名</param> /// <param name="email">当前用户的电子邮件</param> /// <param name="mobile">当前用户的手机号码</param> /// <returns>T/F</returns> public static bool EditUser(string loginName, string displayName, string email, string mobile, string groupName, string userDep = "") { DirectoryEntry currentUser = ADHelper.GetDirectoryEntryByAccount(loginName);//当前被编辑的用户 currentUser.Properties["displayName"][0] = displayName; //currentUser.Properties["name"][0] = displayName;//执行此句会出现错误 if (mobile != "") { if (currentUser.Properties.Contains("telephoneNumber")) { currentUser.Properties["telephoneNumber"][0] = mobile; } else { currentUser.Properties["telephoneNumber"].Add(mobile);//家庭电话otherTelephone } } else if (currentUser.Properties.Contains("telephoneNumber")) { currentUser.Properties["telephoneNumber"].RemoveAt(0); } if (email != "") { if (currentUser.Properties.Contains("mail")) { currentUser.Properties["mail"][0] = email; } else { currentUser.Properties["mail"].Add(email); } } else if (currentUser.Properties.Contains("mail")) { currentUser.Properties["mail"].RemoveAt(0); } if (userDep != "") { if (currentUser.Properties.Contains("department")) { currentUser.Properties["department"][0] = mobile; } else { currentUser.Properties["department"].Add(mobile);//手机号码 } } else if (currentUser.Properties.Contains("department")) { currentUser.Properties["department"].RemoveAt(0); } if (groupName != "") { AddUserToSafeGroup(loginName, groupName); } try { currentUser.CommitChanges(); return(true); } catch (Exception ex) { return(false); } }