下面的方法是可以用来在获取AD group下所有的member的用户信息的一个例子。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | public UserInfoDataSet GetUsersFromADGroup(string path, string userName, string password, string groupName) { UserInfoDataSet dsUsers = new UserInfoDataSet(); try { using (DirectoryEntry de = new DirectoryEntry(path, userName, password)) { DirectorySearcher deSearch = new DirectorySearcher(de); deSearch.Filter = "(&(objectClass=group)(sAMAccountName=" + groupName + "))"; deSearch.PropertiesToLoad.Add("member"); SearchResult r = deSearch.FindOne(); if (r.Properties["member"] == null) { throw new Exception("member propertity is null for the AD group."); } foreach (string dn in r.Properties["member"]) { DirectoryEntry user = new DirectoryEntry(path + "/" + dn, userName, password, AuthenticationTypes.Secure); if (user.SchemaClassName == "user") { string name = GetProperty(user, "sAMAccountName"); string fullName = GetProperty(user, "displayName"); if (!string.IsNullOrEmpty(name)) { UserInfoDataSet.T_COM_USER_INFORRow row = dsUsers.T_COM_USER_INFOR.NewT_COM_USER_INFORRow(); row.USER_ID = -1; row.USER_NAME = name; row.NAME = fullName; row.USER_TYPE = LRMEConstant.UserType.SLA; dsUsers.T_COM_USER_INFOR.Rows.Add(row); } } } } } catch (Exception ex) { throw ex; } return dsUsers; } public string GetProperty(DirectoryEntry entry, string PropertyName) { if (entry != null && entry.Properties.Contains(PropertyName)) { return entry.Properties[PropertyName].Value.ToString(); } else { return string.Empty; } } |
原创文章,转载请注明: 转载自闲云博客
本文链接地址: 从AD中根据group name获取group下所有的用户信息
还没有任何评论。
留下评论







评论暂缺