2013-11-15 38 views
0

我的代碼在我的本地計算機中工作,但在部署到dev服務器後不斷收到錯誤消息。c#:無法獲取用戶的活動目錄組

錯誤消息:

System.ArgumentException: The (&(objectCategory=user)(objectClass=user)(|(userPrincipalName=)(distinguishedName=)(name=))) search filter is invalid. 
     at System.DirectoryServices.SearchResultCollection.ResultsEnumerator.MoveNext() 
     at System.DirectoryServices.SearchResultCollection.get_InnerList() 
     at System.DirectoryServices.SearchResultCollection.get_Count() 
     at System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRefHelper(Type principalType, String urnScheme, String urnValue, DateTime referenceDate, Boolean useSidHistory) 
     at System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRef(Type principalType, String urnScheme, String urnValue, DateTime referenceDate) 
     at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate) 
     at System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, String identityValue) 
     at CLAdmin.Web.Infrastructure.Helpers.ADHelper.GetUserGroups(String userName) 

這裏是我的代碼:在搜索請求中必須包含斷言

public List<string> GetUserGroups(string userName) 
    { 
     var result = new List<string>(); 

     try 
     { 
      using (var context = new PrincipalContext(ContextType.Domain, _ADDomain)) 
      { 
       var user = UserPrincipal.FindByIdentity(context, userName); 

       if (user != null) 
       { 
        //var groups = user.GetAuthorizationGroups(); 
        var groups = user.GetGroups(); 

        foreach (Principal p in groups) 
        { 
         if (p is GroupPrincipal) 
         { 
          result.Add(p.Name); 
         } 
        } 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      _logger.Error("An error happened in GetUserGroups", ex); 
     } 

     return result; 
    } 

回答

1

的方式FindByIdentity方法,你正在使用的作品是(最終某處調用鏈)構建基於查詢你傳遞的用戶名。

鑑於該異常顯示過濾器子句中的空字符串,我會懷疑您傳遞的用戶名是null或空。在方法的頂部添加一個參數檢查來驗證userName參數不爲空或空(我會建議拋出一個System.ArgumentException或它的後代之一的實例,我敢打賭你會看到這個異常,而不是你所描述的那個。

1

過濾器。像(attributeName=)這樣的結構將不起作用,並且如錯誤消息所示,它是無效的。

過濾器或者是

  • 平等:(attributeName=attributeValue)
  • 子:(attributeName=attributeVa*)
  • 本:(attributeName=*)

和一些其他類型。有關過濾器的討論,請參見:

相關問題