简体   繁体   English

如何获得所有Active Directory用户的用户名和显示名称

[英]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() });


        }

The property name you're looking for is sAMAccountName . 您要查找的属性名称是sAMAccountName Here's a list of the available attributes. 这是可用属性的列表。

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.

相关问题 如何从C#中的显示名称获取Active Directory中的用户名? - How to get a username in Active Directory from a display name in C#? 如何删除Active Directory组中的所有用户? - How can I remove all users in an Active Directory group? 如何从活动目录中获取用户列表? - How can I get a list of users from active directory? 如何获得Active Directory中文件夹的用户? - How I get the users of a folder in Active Directory? 如何从使用C#的用户名获取存储在Active Directory中的真实姓名? - How do I get a real name stored in Active Directory from an username with C#? 如何获取Active Directory用户列表(仅Windows登录屏幕中显示的用户) - How can I get a list of Active Directory Users (Only the Users that appear in the windows Logon Screen) 如何从他们的Exchange电子邮件地址获取Active Directory用户名? - How can I get Active Directory username from their Exchange email address? 如何更快地按公用名检索 Active Directory 用户? - How can I retrieve Active Directory users by Common Name more quickly? 如何使用C#按用户名搜索Active Directory? - How can I search Active Directory by username using C#? MVC5如何从Azure Active Directory检索所有用户 - MVC5 How can I retrieve all users from Azure Active Directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM