我读过几篇文章,但我一直没能为自己找到任何东西。我想在“标准用户”OU中创建所有显示名称的数组。然后,我将使用数组列表来填充下拉列表。我在另一个线程中发现了以下内容,但它在指定的OU中提供组而不是用户:
public ArrayList Groups()
{
ArrayList groups = new ArrayList();
foreach (System.Security.Principal.IdentityReference group in
System.Web.HttpContext.Current.Request.LogonUserIdentity.Groups)
{
groups.Add(group.Translate(typeof
(System.Security.Principal.NTAccount)).ToString());
}
return groups;
}是否可以修改它以提供标准用户OU中的用户列表?还是我应该用另一种方法?
我也发现了这一点,但是我得到了一个错误(“std_users”下面的红线),它声明“并非所有的代码路径都返回一个值”。
public ArrayList std_users()
{
// List of strings for your names
List<string> allUsers = new List<string>();
// create your domain context and define the OU container to search in
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "myco.org", "OU=Standard Users, dc=myco, dc=org");
// define a "query-by-example" principal - here, we search for a UserPrincipal (user)
UserPrincipal qbeUser = new UserPrincipal(ctx);
// create your principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
// find all matches
foreach (var found in srch.FindAll())
{
// do whatever here - "found" is of type "Principal" - it could be user, group, computer.....
allUsers.Add(found.DisplayName);
}
}任何帮助都将不胜感激。
我使用的是c#,Visual 2013,框架为4.5.1。
发布于 2015-09-11 19:46:17
将return allUsers;添加到std_users()中,并让我们知道这是否适合您。
发布于 2015-09-11 19:56:36
首先:停止使用 ArrayList --该类型自.NET 2.0以来就是dead,不应该使用--使用List<string> (或者更一般地说:List<T>) --性能和可用性都要好得多!
您可以使用PrincipalSearcher和“逐例查询”主体来执行搜索:
public List<string> GetAllEmailsFromUsersContainer()
{
List<string> users = new List<string>();
// create your domain context and bind to the standard CN=Users
// container to get all "standard" users
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, null, "CN=Users,dc=YourCompany,dc=com"))
{
// define a "query-by-example" principal - here, we search for a UserPrincipal
// which is not locked out, and has an e-mail address
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.IsAccountLockedOut = false;
qbeUser.EmailAddress = "*";
// create your principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
// find all matches
foreach(var found in srch.FindAll())
{
users.Add(found.EmailAddress);
}
}
return users;
}https://stackoverflow.com/questions/32530779
复制相似问题