【问题标题】:Get all users from active directory in NetCore 2.0从 NetCore 2.0 的活动目录中获取所有用户
【发布时间】:2019-04-10 10:57:37
【问题描述】:

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

【问题讨论】:

    标签: c# asp.net-core active-directory ldap novell


    【解决方案1】:

    我正在使用 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;
    }
    

    配置.json

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

    【讨论】:

      【解决方案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 class

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-11-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多