UserPrincipal.GetGroups失败,未知错误

UserPrincipal.GetGroups失败,未知错误

问题描述:

我试图让所有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

添加Environment.UserDomainName作为名称参数PrincipalContext帮助:

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

我仍然不知道为什么PrincipalContext(ContextType.Domain)只适用于寻找UserPrincipal而不是用户的组。 COM错误消息“未知错误”不是很有帮助,只有ContextType的PrincipalContext构造函数重载实际上在MSDN上没有记录。正如Harvey Kwok所指出的那样,它听起来像是一个.NET框架的问题。