[英]How can I get all Active Directory Users username and display name
I have an Intranet application using ASP.Net MVC 5 我有一个使用ASP.Net MVC 5的Intranet应用程序
I need to query all Active directory users username 我需要查询所有Active Directory用户的用户名
I searched the net I found 2 useful post: 1- How can I get a list of users from active directory? 我在网上搜索了2条有用的文章:1- 如何从活动目录中获取用户列表?
2- http://www.codeproject.com/Tips/599697/Get-list-of-Active-Directory-users-in-Csharp 2- http://www.codeproject.com/Tips/599697/Get-list-of-Active-Directory-users-in-Csharp
When I query I can get the Name property but I can't get the active directory usernames 查询时,我可以获取Name属性,但无法获取活动目录用户名
any solutions that I can get all Active directory users username? 我可以获取所有Active Directory用户用户名的任何解决方案?
bellow is my code: 下面是我的代码:
List<TempUsers> MyTempUser = new List<TempUsers>();
var context = new PrincipalContext(ContextType.Domain, "MyDomain.com");
var searcher = new PrincipalSearcher(new UserPrincipal(context));
foreach (var result in searcher.FindAll())
{
DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
MyTempUser.Add(new TempUsers { UserName = de.Properties["Name"].Value.ToString() });
}
I did it this way it worked great: 我这样做的方式非常有效:
1- Add reference to Active Directory services DLL 1-添加对Active Directory服务DLL的引用
2- Add using in your controller: 2-在您的控制器中添加使用:
using System.DirectoryServices.AccountManagement;
than I created a function to store All Active directory users in database table bellow is the code hope help someone needs it. 比我创建了一个将所有Active Directory用户存储在数据库表中的功能,下面的代码是希望帮助某人需要的代码。
public ActionResult Create()
{
List<MyADUsers> TheAllADUsers = new List<MyADUsers>();
var context = new PrincipalContext(ContextType.Domain, "MyDoman.org");
var searcher = new PrincipalSearcher(new UserPrincipal(context));
foreach (var result in searcher.FindAll())
{
TheAllADUsers.Add(new MyADUsers { ADUserName = result.SamAccountName, AD_IsMemberOf = result.UserPrincipalName, FullName = result.Name });
}
db.MyADUsersContext.AddRange(TheAllADUsers);
db.SaveChanges();
return View();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.