0

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

4

3 回答 3

2

我使用的是 .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 类

于 2020-07-20T16:01:42.037 回答
2

我正在使用 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]"
 }
于 2018-11-07T04:07:03.463 回答
1

如果您只计划在 Windows 中运行您的应用程序,您可以Microsoft.Windows.Compatibility从 NuGet 添加到您的项目,其中包括System.DirectoryServices命名空间,因此您可以像在完整的 .NET Framework 中一样使用DirectoryEntry/DirectorySearcher或命名空间。AccountManagement

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

于 2018-11-07T13:23:41.827 回答