Solved

If condition in search filter

Posted on 2011-02-21
5
1,082 Views
Last Modified: 2012-05-11
Hi ,

I wrote a below code :

using System;
using System.Text;
using System.DirectoryServices;

namespace activeDirectoryLdapExamples
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.Write("Enter user: ");
            String username = Console.ReadLine();

            if (username.Contains("."))
            {
               
                string[] splitString = username.Split(new char[] { '.' });
                username = splitString[splitString.Length - 1];
                Console.WriteLine(username);


            }


            try
            {
                // create LDAP connection object

                // DirectoryEntry myLdapConnection = createDirectoryEntry();

                DirectoryEntry entry = new DirectoryEntry("GC://Test", "Test\\Test", "Ld@Test", AuthenticationTypes.Secure);

                // create search object which operates on LDAP connection object
                // and set search object to only find the user specified

                // DirectorySearcher search = new DirectorySearcher(myLdapConnection);
                System.DirectoryServices.DirectorySearcher search = new System.DirectoryServices.DirectorySearcher(entry);
                //  search.Filter = "(cn=" + username + ")";

                search.Filter = "(sn=" + username + ")";
                // create results objects from search object

                SearchResult result = search.FindOne();

                if (result != null)
                {

                    // user exists, cycle through LDAP fields (cn, telephonenumber etc.)

                    ResultPropertyCollection fields = result.Properties;

                    foreach (String ldapField in fields.PropertyNames)
                    {
                        // cycle through objects in each field e.g. group membership
                        // (for many fields there will only be one object such as name)

                        foreach (Object myCollection in fields[ldapField])
                            Console.WriteLine(String.Format("{0,-20} : {1}", ldapField, myCollection.ToString()));

                    }
                }

                else
                {
                    // user does not exist
                    Console.WriteLine("User not found!");
                }
            }

            catch (Exception e)
            {
                Console.WriteLine("Exception caught:\n\n" + e.ToString());
            }
        }


    }
}

Am passing the string (eg:will.smith) in that case am using IF statement to get the last name (eg:smith) and using search.Filter = "(sn=" + username + ")";

Now i have a problem,if the string comes like (eg: Will(space )Smith) then i will take the whole string and use the  search.Filter = "(cn=" + username + ")";

i was trying to put if statement,it doesn't work,some one please give me an idea.



0
Comment
Question by:Sha1395
  • 2
  • 2
5 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 34949201
You can search for the space location....

// DirectorySearcher search = new DirectorySearcher(myLdapConnection);

System.DirectoryServices.DirectorySearcher search = new System.DirectoryServices.DirectorySearcher(entry);

if (username.IndexOf(" ") > 0)
     search.Filter = "(cn=" + username + ")";
else
      search.Filter = "(sn=" + username + ")";
               
 // create results objects from search object

0
 
LVL 8

Expert Comment

by:crysallus
ID: 34949268
Probably easiest if you create the whole filter search string in the if statement eg.

string searchFilter = "";
if (username.Contains("."))
{
    string[] splitString = username.Split(new char[] { '.' });
    searchFilter = "(sn=" + splitString[splitString.Length - 1] + ")";
}
else if (username.Contains(' '))
{
    string[] splitString = username.Split(new char[] { ' ' });
    searchFilter = "(sn=" + splitString[splitString.Length - 1] + ")";
}
else
    searchFilter = "(cn=" + username + ")";

try
{
    ... (your code as is here)
    search.Filter = searchFilter;
etc.

Open in new window

As long as that matches what you want to search on i.e. surname if username contains it, otherwise the whole username.
0
 

Author Comment

by:Sha1395
ID: 34950193
Thanks Dhaest and crysallus for your great reply,i just modified my code based on Dhaet (feel very simple and very less to change in my code)

Anyway thanks a lot guys
0
 

Author Comment

by:Sha1395
ID: 34950235
i have one more question the whole code was written in C# console application ,i just want to convert this file in to DLL class library and i will create windows application and pass the string from there.

Please help me or suggest me a simple way to convert the whole file in to DLL class library then i will call this dll from my win application and pass the string from there.

Thanks in Advance
0
 
LVL 8

Expert Comment

by:crysallus
ID: 34950253
That question you've just added probably needs to be a separate question so you can assign more points to whoever answers it.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 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

13 Experts available now in Live!

Get 1:1 Help Now