AD query

Last post 07-07-2009 6:41 PM by FileFoundException. 12 replies.

Sort Posts:

  • AD query

    06-25-2009, 7:33 PM
    • Member
      5 point Member
    • Munna1980
    • Member since 03-19-2009, 7:22 PM
    • Posts 32

    Hi All,

    I am trying to find whether a user is present in AD or not. I have following code.

    public static DirectoryEntry GetDirectoryEntry()
    {
       DirectoryEntry de = new DirectoryEntry();
       de.Path = "LDAP://OU=net.org,DC=net,DC=org";
       de.AuthenticationType = AuthenticationTypes.Secure;

       return de;
    }

    public bool UserExists(string username)
    {
       DirectoryEntry de = GetDirectoryEntry();
       DirectorySearcher deSearch = new DirectorySearcher();

       deSearch.SearchRoot = de;
       deSearch.Filter = "(&(objectClass=user) (cn=" + username + "))";

       SearchResultCollection results = deSearch.FindAll();

       return results.Count > 0;
    }

    private String FindName(String userAccount)
    {
       DirectoryEntry entry = GetDirectoryEntry();
       String account = userAccount.Replace(@"enterprisenet\", "");

       try
       {
          DirectorySearcher search = new DirectorySearcher(entry);
          search.Filter = "(SAMAccountName=" + account + ")";
          search.PropertiesToLoad.Add("displayName");

          SearchResult result = search.FindOne();

          if (result != null)
          {
             return result.Properties["displayname"][0].ToString();
          }
          else
          {
             return "Unknown User";
          }
       }
       catch (Exception ex)
       {
          string debug = ex.Message;

          return "";
       }
    }

     

    I am not sure what i am missing here. when i built the code it returned me blank page. Can anyone help how to display the results here on the page?.

    thank you.

  • Re: AD query

    06-26-2009, 12:28 AM

    Try replacing this line 

    deSearch.Filter = "(&(objectClass=user) (cn=" + username + "))";

    as follows

    deSearch.Filter = "(&(objectClass=user) (sAMAccountName=" + username + "))";

    because you're searching by username and not by the common name.


    There are two ways to write error-free programs; only the third one works. ["Epigrams in Programming", by Alan J. Perlis]
  • Re: AD query

    06-26-2009, 8:11 AM
    • Member
      5 point Member
    • Munna1980
    • Member since 03-19-2009, 7:22 PM
    • Posts 32

    Hi,

    Thank for your response. actually i am trying to find whether the specific user is present or not. i have tried replacing the code provided by you no  change. But my problem is i need to show the results on label or in textbox. i am not sure how to show the results. Below code is i think trying to find the

    user present or not. if present where i have display. please help.

    private String FindName(String userAccount)
    {
       DirectoryEntry entry = GetDirectoryEntry();
       String account = userAccount.Replace(@"net\", "");

       try
       {
          DirectorySearcher search = new DirectorySearcher(entry);
         search.Filter = "(SAMAccountName=" + account + ")";

          search.PropertiesToLoad.Add("displayName");

          SearchResult result = search.FindOne();

          if (result != null)
          {
             return result.Properties["displayname"][0].ToString();
          }
          else
          {
             return "Unknown User";
          }
       }
       catch (Exception ex)
       {
          string debug = ex.Message;

          return "";
       }
    }

  • Re: AD query

    06-26-2009, 9:43 AM

    What's the error you're getting? I tried your code and it works just fine.

    There are two ways to write error-free programs; only the third one works. ["Epigrams in Programming", by Alan J. Perlis]
  • Re: AD query

    06-26-2009, 10:29 AM
    • Member
      5 point Member
    • Munna1980
    • Member since 03-19-2009, 7:22 PM
    • Posts 32

    Hi,

    I have .aspx file and i have included all the above code. when i accessed the page it showing me blank page. i am dont know how to show results on the page. can you please provide the working code you had. thanks

     

     

     

     

     

  • Re: AD query

    06-26-2009, 12:02 PM

    OK. Your problem might be that your web app (.aspx page) is not allowed to query the AD. Try this, in your catch statements throw the exception for the time being since it's still in dev and not in production

    catch (Exception ex)
       {

          string debug = ex.Message;      

          throw ex;


       }

    And see what exception you get. Alternatively, you can also step through and see the exception message is.


    There are two ways to write error-free programs; only the third one works. ["Epigrams in Programming", by Alan J. Perlis]
  • Re: AD query

    06-26-2009, 3:45 PM
    • Member
      5 point Member
    • Munna1980
    • Member since 03-19-2009, 7:22 PM
    • Posts 32

    Hi,

    can you help me to display the user  names once i find in AD.

    thanks.

  • Re: AD query

    06-27-2009, 12:32 PM

    Sure. But I don't know exactly what you mean. Have resovled the previous error with your code?

    You can add AD properties to load just as you did with the displayName

    search.PropertiesToLoad.Add("sAMAccountName");

    Check out ADExplorer tool from http://technet.microsoft.com/en-us/sysinternals/bb963907.aspx this a great tool [for discovering your AD]


    There are two ways to write error-free programs; only the third one works. ["Epigrams in Programming", by Alan J. Perlis]
  • Re: AD query

    06-29-2009, 11:57 AM
    • Member
      5 point Member
    • Munna1980
    • Member since 03-19-2009, 7:22 PM
    • Posts 32

    Actually it seems i am unable to authenticate AD properly. I am not sure how to confirm on that.

    My code looks good but it is showing blank page without any results.

    I am googling to find disabled users from AD and thier managers. let me see if i can get some luck.

    thank you so much any help is appreciated.

  • Re: AD query

    06-29-2009, 3:50 PM

    Remove try/catch statements for the time being or step through your program and see what exceptions are thrown to verify that the problem is you're not able to query the active directory. If so, you'll need to enable impersonation in your web.config.

    Here is a link describing how to get a list of disable AD accounts

    http://www.microsoft.com/technet/scriptcenter/resources/qanda/may05/hey0512.mspx


    There are two ways to write error-free programs; only the third one works. ["Epigrams in Programming", by Alan J. Perlis]
  • Re: AD query

    06-29-2009, 5:34 PM

    I modified your previous code so that it now enumerates all disabled accounts and load their manager's properties. I have not done this in production, so I am not sure if this is the best way to find manager's properties; but it works

    public static DirectoryEntry GetDirectoryEntry()

    {

    DirectoryEntry de = new DirectoryEntry();

    de.Path = "LDAP://" your path here;

    de.AuthenticationType = AuthenticationTypes.Secure;

    return de;

    }

    public static void GetDisabledAccountsAndTheirManagers()

    {

    DirectoryEntry entry = GetDirectoryEntry();

    try

    {

    DirectorySearcher search = new DirectorySearcher(entry);

    search.Filter = "(&(objectCategory=User)(userAccountControl:1.2.840.113556.1.4.803:=2))";

    search.PropertiesToLoad.Add("displayName");

    search.PropertiesToLoad.Add("Manager");

    SearchResultCollection resultCollection = search.FindAll();

    foreach (SearchResult result in resultCollection )

    {

    if (result.Properties.Contains("displayName"))

    {

    Console.WriteLine(result.Properties["displayName"][0]);

    }

    if (result.Properties.Contains("Manager"))

    {

    Console.WriteLine(result.Properties["Manager"][0]);

    DirectoryEntry managerDirEntry = new DirectoryEntry("LDAP://" + result.Properties["Manager"][0]);

    if (managerDirEntry != null)

    {

    if (managerDirEntry.Properties.Contains("displayName"))

    {

    Console.Write("Manager's Diplay Name: " + managerDirEntry.Properties["displayName"][0].ToString());

    }

    if(managerDirEntry.Properties.Contains("email"))

    {

    Console.Write("Manager's email: " + managerDirEntry.Properties["email"][0].ToString());

    }

    Console.WriteLine();

    }

    }

    }

    }

    catch (Exception ex)

    {

    string debug = ex.Message;

    throw ex;

    }

    }

    There are two ways to write error-free programs; only the third one works. ["Epigrams in Programming", by Alan J. Perlis]
  • Re: AD query

    07-04-2009, 5:35 PM
    • Member
      5 point Member
    • Munna1980
    • Member since 03-19-2009, 7:22 PM
    • Posts 32

    Hi,

    Thank you very much and i really really appreciated for you help here.

    Thanks. 

  • Re: AD query

    07-07-2009, 6:41 PM

    You're welcome. Did any of the above work for you? If so can you close this question?

    There are two ways to write error-free programs; only the third one works. ["Epigrams in Programming", by Alan J. Perlis]
Page 1 of 1 (13 items)