繁体   English   中英

从 NetCore 2.0 中的活动目录中获取所有用户

[英]Get all users from active directory in NetCore 2.0

所有问题都与 .NET 框架有关,但与 .NET Core 无关。 我正在寻找如何从 NETCORE 中的 AD 组获取所有用户信息。

我正在使用 Novell.Directory.Ldap 包连接到 Ldap 以验证我的用户。

项目.csproj

<PackageReference Include="Novell.Directory.Ldap.NETStandard" Version="2.3.8" />

代码.cs

using Novell.Directory.Ldap;

public bool LoginLdap(string username, string password)
{
    LdapConnection connection = new LdapConnection();
    var loggedIn = false;
    try
    {
         connection.Connect(_config["Ldap:url"], LdapConnection.DEFAULT_PORT);
         connection.Bind(LdapConnection.Ldap_V3, _config["Ldap:domain"] + @"\" + username, password);
         loggedIn = true;
    }
    catch 
    {
         loggedIn = false;
    }
    connection.Disconnect();
    return loggedIn;
}

配置文件

"Ldap": {
"url": "[Ldap URL]",
"domain": "[Domain Name]"
 }

我使用的是 .Net Core 3.1,但您也可以使用 .Net Core 2。

首先安装 NuGet 包“System.DirectoryServices.AccountManagement”

然后,您可以使用以下代码获取所有 AD 用户:

using System.DirectoryServices.AccountManagement; 


public static List<ADUser> GetADUsers() {

    var myDomainUsers = new List<ADUser>();

    using (var ctx = new PrincipalContext(ContextType.Domain, "yourdomain"))
    {

        var userPrinciple = new UserPrincipal(ctx);

        using (var search = new PrincipalSearcher(userPrinciple))
        {
            foreach (UserPrincipal domainUser in search.FindAll().OrderBy(u => u.DisplayName))
            {
                var adUser = new ADUser() {
                    Description = domainUser.Description,
                    DisplayName = domainUser.DisplayName,
                    DistinguishedName = domainUser.DistinguishedName,
                    EmailAddress = domainUser.EmailAddress,
                    Name = domainUser.Name,
                    EmployeeId = domainUser.EmployeeId,
                    GivenName = domainUser.GivenName,
                    MiddleName = domainUser.MiddleName,
                    Surname = domainUser.Surname,
                    SamAccountName  = domainUser.SamAccountName
                }; 
                myDomainUsers.Add(adUser);
            } //foreach
        } //using
    } //using

    return myDomainUsers;

} //GetADGroups

我在哪里使用以下 ADUser 类:

public class ADUser
{
    public string SamAccountName { get; set; }
    public string Description { get; set; }
    public string DisplayName { get; set; }
    public string DistinguishedName { get; set; }
    public string EmailAddress { get; set; }
    public string EmployeeId { get; set; }
    public string Name { get; set; }
    public string GivenName { get; set; }
    public string MiddleName { get; set; }
    public string Surname { get; set; }

}

您可以从 AD 中提取更多属性。 看看UserPrincipal 类

如果你只计划在 Windows 中运行你的应用程序,你可以从 NuGet 添加Microsoft.Windows.Compatibility到你的项目,其中包括System.DirectoryServices命名空间,这样你就可以像完整的一样使用DirectoryEntry / DirectorySearcherAccountManagement命名空间.NET 框架。

但是如果你打算在其他操作系统上运行它,那么我认为唯一的选择是 Novell 的库,正如史蒂夫在他的回答中提到的那样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM