我的代碼在我的本地計算機中工作,但在部署到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;
}