Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
Hi Guru's

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 in search filter but it doesn't work,some one please give me an idea.

[edit]Code Block added - OriginalGriff[/edit]
Posted
Updated 21-Feb-11 21:06pm
v5
Comments
Sergey Alexandrovich Kryukov 21-Feb-11 22:53pm    
Do you repeat your previous question? You should use "Improve question" instead.
--SA
shan1395 21-Feb-11 23:12pm    
Its pretty much similar,even used "improve question"but not sure ,thrown an error,that's why i choosed New Question
TweakBird 21-Feb-11 22:57pm    
Edited for code formatting. Please use <pre> tag for code blocks.
Henry Minute 21-Feb-11 23:13pm    
Please do not keep posting the same question.
Bevin wc 12-Oct-17 8:16am    
Can someone help me with this ? https://www.codeproject.com/Questions/1210193/Search-users-by-filtering-using-whencreated-in-act

1 solution

If you are sure that there always will be one character separating the names you can use following snippet instead of your if clause.
char[] splitChars = new char[] {'.', ' ', ',', ';', ':',...};
if (username.IndexOfAny(splitChars) != -1)
{
    string[] splitString = username.Split(splitChars);
    username = splitString[splitString.Length - 1];
    Console.WriteLine(username);
}


Otherwise you should try to use RegEx to perform matching.
 
Share this answer
 
Comments
Bevin wc 12-Oct-17 8:16am    
Can someone help me with this ? https://www.codeproject.com/Questions/1210193/Search-users-by-filtering-using-whencreated-in-act

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900