簡體   English   中英

無法使用 IIS 托管應用程序上的 System.DirectoryServices.AccountManagement 獲取 LDAP 數據

[英]Cannot get LDAP data using System.DirectoryServices.AccountManagement on IIS hosted app

我正在使用 System.DirectoryServices.AccountManagement Package 從 LDAP 獲取用戶數據,它在我的本地機器上運行良好,但在服務器上,它給出了錯誤消息:

**發生操作錯誤。

at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
   at System.DirectoryServices.DirectoryEntry.Bind()
   at System.DirectoryServices.DirectoryEntry.get_AdsObject()
   at System.DirectoryServices.PropertyValueCollection.PopulateList()
   at System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName)
   at System.DirectoryServices.PropertyCollection.get_Item(String propertyName)
   at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer()
   at System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit()
   at System.DirectoryServices.AccountManagement.PrincipalContext.Initialize()
   at System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx()
   at System.DirectoryServices.AccountManagement.PrincipalSearcher.SetDefaultPageSizeForContext()
   at System.DirectoryServices.AccountManagement.PrincipalSearcher..ctor(Principal queryFilter)  

**

.NET 核心版本是 3.1,System.DirectoryServices.AccountManagement 版本是 6.0,應用程序托管在 VM 上的 IIS(IIS 版本 8.0)上。 我正在使用的代碼是:

List < ApplicationUser > users = new List < ApplicationUser > ();

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

  var userPrinciple = new UserPrincipal(ctx);
  using(var search = new PrincipalSearcher(userPrinciple)) {
    var results = search.FindAll().OrderBy(u => u.DisplayName);
    foreach(UserPrincipal domainUser in results) {
      var adUser = new ApplicationUser {
          Email = domainUser.EmailAddress,
          FirstName = domainUser.Name,
          PhoneNumber = domainUser.VoiceTelephoneNumber,
          UserName = domainUser.UserPrincipalName,
          EmployeeId = domainUser.EmployeeId
      };

      if (!String.IsNullOrWhiteSpace(adUser.Email)) {
        users.Add(adUser);
      }

    }
  }
}

到目前為止,我已經嘗試將應用程序池身份更改為網絡服務,但沒有奏效。

堆棧跟蹤顯示異常發生在DirectoryEntry.Bind中,這是它最初連接到 AD 的時候。 因此,您的計算機和服務器之間必須有所不同。

服務器是否可以訪問域? 有防火牆阻止訪問嗎?

您的計算機是否已加入域,但服務器未加入? 如果是這種情況,請嘗試在PrincipalContext構造函數中使用域的完整 DNS 名稱,如果您還沒有使用(例如mydomain.com而不僅僅是mydomain )。

您是否在服務器上使用模擬? 如果是這樣,那可能就是問題所在。 見這里: https://stackoverflow.com/a/21547199/1202807

那是解決 ASP.NET (不是核心),但它可能是一個類似的問題。

經過大量搜索,我找到了一個替代解決方案:

List<ApplicationUser> users = new List<ApplicationUser>();
       
        using (var root = new DirectoryEntry(_ladapSettings.LDAPPathString, _ladapSettings.UserName, _ladapSettings.Password))
        {
            using (var searcher = new DirectorySearcher(root))
            {
                searcher.Filter = $"(&(objectCategory=person)(objectClass=user)(|(displayName=*{filter})(displayName={filter}*)))";
                var query = searcher.FindAll();

                foreach (SearchResult results in query)
                {
                    var de = results.GetDirectoryEntry();

                    if(de != null)
                    {

                        var adUser = new ApplicationUser
                        {
                            Email = de.Properties["mail"].Value?.ToString(),
                            UserName = de.Properties["mail"].Value?.ToString(),
                            FirstName = de.Properties["givenname"].Value?.ToString(),
                            LastName = de.Properties["sn"].Value?.ToString(),
                            PhoneNumber = de.Properties["telephoneNumber"].Value?.ToString(),
                            EmployeeId = de.Properties["employeeid"].Value?.ToString()
                        };

                        if (!String.IsNullOrWhiteSpace(adUser.Email))
                        {
                            users.Add(adUser);
                        }

                    }
                }
            }

這在本地機器和服務器上都可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM