Solved

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

Posted on 2010-08-13
6
414 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:KazooSoft
  • 3
  • 3
6 Comments
 
LVL 5

Expert Comment

by:JayFromPep
ID: 33429941
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.
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 33430483

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

Chris
0
 
LVL 5

Expert Comment

by:JayFromPep
ID: 33430677
Not sure.  Sounds like a good option.  What would your approach be Chris?
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 33430764

I'd want to limit connections to the directory if the number of accounts it has to look at is unknown. I'd probably try something along these lines.

Although it's always possible there's a better way :)

Chris
public static bool Object_Exists(string SearchRoot, string LdapFilter) 
{
  // Test values
  // String LdapFilter = "(name=*333333)";
  // DirectoryEntry SearchRoot = new DirectoryEntry("LDAP://OU=this,OU=that,DC=domain,DC=local");

  DirectoryEntry SearchRoot = new DirectoryEntry("LDAP://" + SearchRoot);
  DirectorySearcher Searcher = new DirectorySearcher(SearchRoot, LdapFilter);
  Searcher.SearchScope = SearchScope.OneLevel

  SearchResultCollection Results = Searcher.FindAll();

  If (Results.Count > 0) {
    return true;
  }
  return false;
}

Open in new window

0
 
LVL 5

Expert Comment

by:JayFromPep
ID: 33430866
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.
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 33430901

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
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Resolve DNS query failed errors for Exchange
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now