(&(objectCategory = user)(|(CN =)(sAMAccountName =)))搜索过滤器无效

时间:2012-07-06 14:44:17

标签: c# asp.net active-directory

  

(&(objectCategory = user)(|(CN =)(sAMAccountName =)))搜索过滤器是   无效。

有人能告诉我这个错误出现在什么情况下?

我正在尝试通过Visual Studio发布我的Web应用程序。我在本地主机上运行正常,但在发布后出现此错误。

 public static string[] ldapUserQuery(string userName, string[] desiredProps)
    {
        //  Establishes path for query. In this case sap.corp
        string queryPath = "LDAP://DC=SAP,DC=CORP";

        //  Used to establish desiredProps from within the function, but now it can be passed in.
        //  This was kept in here as an example of what the desiredProps should look like when
        //  passing it in.
        //
        //  Desired pros are the LDAP attributes to be returned.
        /*
            string[] desiredProps = new string[]
            {
                //"givenName",    //  first name
                //"sn",           //  last name
                //"mail"          //  email
            };
        */

        //  establishes the proper length of the array to be returned.
        string[] returnVal = new string[desiredProps.Length];

        //  Creates objects for performing the Query.
        DirectoryEntry dirEntry = new DirectoryEntry();
        DirectorySearcher dirSearcher = new DirectorySearcher();
        SearchResult result;

        //  dirEntry is used for establishing the connection for the query
        dirEntry.Path = queryPath;
        dirEntry.AuthenticationType = AuthenticationTypes.Secure;

        //  dirSearcher is the query itself.
        dirSearcher.SearchRoot = dirEntry;
        dirSearcher.Filter = string.Format("(&(objectCategory=user)(|(CN={0})(sAMAccountName={0})))", userName);

        //  executes the query.
        result = dirSearcher.FindOne();

        //  handle the result of the query.
        if (result != null)
        {
            //  if the query was successful, then dig through the returned information
            //  and set found objects that match the desiredProps to the returnVal object.
            for (int i = 0; i < desiredProps.Length; i++)
            {
                //  if the desiredProps has a bad attribute name, just give it the placeholder
                //  and move along.
                if (result.Properties[desiredProps[i]] != null)
                {
                    returnVal[i] = result.GetDirectoryEntry().Properties[desiredProps[i]].Value.ToString();
                }
                //else
                //{
                //    returnVal[i] = GlobalVars.placeHolder;
                //}
            }
        }

1 个答案:

答案 0 :(得分:5)

(&(objectCategory=user)(|(CN=)(sAMAccountName=)))无效,因为它不知道您要与之比较的CN属性和sAMAccountName属性。

(&(objectCategory=user)(|(CN=something)(sAMAccountName=someName)))有效。用对你有意义的东西替换某些东西 someName