Link to home
Start Free TrialLog in
Avatar of KazooSoft
KazooSoftFlag for United Kingdom of Great Britain and Northern Ireland

asked on

c# Search AD for user account only knowing a section of it! Help!

Hey all again. Another Active Directory c# problem. See th pattern? lol.

Ok, now out student accounts contain whats known as a MISID, I need to search the AD looking for any account with reference to this in the CN. for example,

SC10333333

My MISID would be 333333, now i know the user would be SC10333333, but they may also be SC093333, SC083333 etc.. So i just want to search by the MISID.

How can this be done, I have tried the following but it doesn't work:


/// <summary>
        /// Check for the existence of an Object
        /// </summary>
        /// <param name="objectPath"></param>
        /// <returns>True or False</returns>
        public static bool Object_Exists(string CN=*333333,OU=this,OU=that,DC=domain,DC=local)
        {
            bool found = false;
            if (DirectoryEntry.Exists("LDAP://" + objectPath))
            {
                found = true;
            }
            return found;
        }

Open in new window

Avatar of JayFromPep
JayFromPep
Flag of United States of America image

It wont work because it is a string, and therefore the '*' is taken literally.

What you can do is create a for loop, creating the CN string in iterations.

IE
public foo(string MISID)
      {
        int i;
        string cn;
        for (i = 0; i <= 10; i++)
        {
            cn = Convert.ToString(i) + MISID;
        }
        return cn;
      }

Use this return value in your logic to do the search.  That way it iterates through until you find one that matches, then jumps on to the next proc.
Avatar of Chris Dent

You wouldn't be better setting up a search? That would allow you to use the wildcard.

Chris
Not sure.  Sounds like a good option.  What would your approach be Chris?
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Chris,

Let me start by saying 'Duh'.....should have thought it through mo' betta.

I like it.  I think at the end to make the results work for the poster, you would have to return something to work with other than a bool.  Probably an array of some sort that has the items in it.
That array could then be itterated through to find the information of interest.

nice job.

Yeah, definitely, I only went for bool in the return to replicate the intention of the original. I'd probably have it return a small number of properties in the SearchResultCollection for use outside the method.

Chris