发布网友 发布时间:2022-04-23 02:41
共1个回答
热心网友 时间:2023-05-18 13:30
public DataSet GetUsersForGroup(string GroupName) { //if (GroupName.IndexOf(';') > 0) //{ // return GetUsersForGroup(GroupName.Split(new char[] { ';' })); //} if (GroupName == string.Empty) return null; try { DirectoryEntry dir = GetDirectoryObject(); dir.RefreshCache(); DirectorySearcher ds = new DirectorySearcher(dir); ds.Filter = "(&(objectClass=group)(cn=" + GroupName + "))"; ds.PropertiesToLoad.Add("memberof"); SearchResult results = ds.FindOne(); DataSet dsUser = new DataSet(); DataTable tbUser = dsUser.Tables.Add("Users"); tbUser.Columns.Add("GroupName"); tbUser.Columns.Add("samaccountname"); tbUser.Columns.Add("UserName"); tbUser.Columns.Add("DisplayName"); tbUser.Columns.Add("EMailAddress"); tbUser.Columns.Add("primaryGroupID"); if (results != null) { DirectoryEntry deGroup = new DirectoryEntry(results.Path, _ADUser, _ADPwd, AuthenticationTypes.Secure); System.DirectoryServices.PropertyCollection pcoll = deGroup.Properties; int n = pcoll["member"].Count; for (int i = 0; i < n; i++) { DirectoryEntry deUser = new DirectoryEntry(_ADPath + "/" + pcoll["member"][i].ToString(), _ADUser, _ADPwd, AuthenticationTypes.Secure); try { DataRow rwUser = tbUser.NewRow(); rwUser["GroupName"] = GroupName; rwUser["samaccountname"] = GetProperty(deUser, "samaccountname"); rwUser["UserName"] = GetProperty(deUser, "cn"); rwUser["DisplayName"] = GetProperty(deUser, "givenName") + " " + GetProperty(deUser, "sn"); rwUser["EMailAddress"] = GetProperty(deUser, "mail"); rwUser["primaryGroupID"] = GetProperty(deUser, "primaryGroupID"); tbUser.Rows.Add(rwUser); deUser.Close();