2010-12-23 118 views
17

我試圖讓所有Active Directory組的用戶,用下面的代碼:UserPrincipal.GetGroups失敗,未知錯誤

private static IEnumerable<string> GetGroupNames(string userName) 
    { 
     using (var context = new PrincipalContext(ContextType.Domain)) 
     { 
      using (var userPrincipal = UserPrincipal.FindByIdentity(context, userName)) 
      { 
       var groupSearch = userPrincipal.GetGroups(context); 
       var result = new List<string>(); 
       foreach (var principal in groupSearch) 
       { 
        Log.LogDebug("User {0} is member of group {0}", userPrincipal.DisplayName, principal.DisplayName); 
        result.Add(principal.SamAccountName); 
       } 
       return result; 
      } 
     } 
    } 

此代碼正確地找到用戶主體,但是當GetGroups被調用失敗一個PrincipalOperationException:未知的錯誤(0x80005000)。

根異常:

at System.DirectoryServices.AccountManagement.ADStoreCtx.GetGroupsMemberOf(Principal foreignPrincipal, StoreCtx foreignContext) 
    at System.DirectoryServices.AccountManagement.Principal.GetGroupsHelper(PrincipalContext contextToQuery) 
    at System.DirectoryServices.AccountManagement.Principal.GetGroups(PrincipalContext contextToQuery) 
    at [line of the GetGroup call] 

內部異常(收到COMException):

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.ADUtils.RetriveWkDn(DirectoryEntry deBase, String defaultNamingContext, String serverN 

Another report with this problem

任何線索?

+1

這是否發生在所有用戶?或者它只發生在一個特定的用戶?我知道.NET庫中存在一個錯誤,當用戶DN包含「/」時,它會引發此COMException。如果您確認這種情況只發生在僅包含「/」的DN的用戶身上,我也對此問題進行了修復。 – 2010-12-24 07:32:11

+0

我遇到您描述的問題。當用戶DN包含「/」時,我遇到問題。你能告訴我你使用的修復程序是什麼? – Lamelas84 2017-01-31 09:04:31

回答

29

添加Environment.UserDomainName作爲名稱參數PrincipalContext幫助:

using (var context = new PrincipalContext(ContextType.Domain, Environment.UserDomainName)) 

我仍然不知道爲什麼PrincipalContext(ContextType.Domain)只適用於尋找UserPrincipal而不是用戶的組。 COM錯誤消息「未知錯誤」不是很有幫助,只有ContextType的PrincipalContext構造函數重載實際上在MSDN上沒有記錄。正如Harvey Kwok所指出的那樣,它聽起來像是一個.NET框架的問題。

相關問題