fetch active directory user data using C#

倾然丶 夕夏残阳落幕 提交于 2021-02-06 14:00:47

问题


I want to fetch data about particular user . I know the OU path of this user but I can't fetch info using that OU path . It always says that user is not found . Can anyone tell me that do I need to change search filter . Please help .

Code

path of the user abc.ds.xyz.net/fGroup/xcxc/Users/123456 abc.ds.xyz.net is domain then fGroup is OU , xcxc is OU , Users is OU , 123456 is cn .

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.DirectoryServices;
    using System.DirectoryServices.AccountManagement;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

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

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

            try
            {
              DirectoryEntry myLdapConnection = createDirectoryEntry();

              DirectorySearcher search = new DirectorySearcher(myLdapConnection);
              search.Filter = "(&(OU=fGroup)(OU=xcxc )(OU=Users)(cn=" + username + "))";
              SearchResult result = search.FindOne();

              if (result != null)
              {

                ResultPropertyCollection fields = result.Properties;

                foreach (String ldapField in fields.PropertyNames)
                {

                    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!");
                Console.ReadLine();
             }
         }

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


    }
    static DirectoryEntry createDirectoryEntry()
    {
        DirectoryEntry ldapConnection = new DirectoryEntry("abc.ds.xyz.net");
        ldapConnection.Path = "LDAP://DC=abc,DC=ds,DC=xyz,DC=net";
        ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
        return ldapConnection;
    }
   }
  }

EDIT

    search.Filter = "(&(OU=fGroup))";
    SearchResult result = search.FindOne();

when I change search.Filter = "(&(OU=fGroup)(OU=xcxc )(OU=Users)(cn=" + username + "))"; into search.Filter = "(&(OU=fGroup))"; I get result . Can anyone tell me how to search with multiple search input filter .


回答1:


After hitting my head against a wall for hours and hours finally I found answer . I need to write multiple search filters like

Old code search.Filter = "(&(OU=fGroup))"; replace this with this

   search.Filter = "(&(OU=fGroup))";
   search.Filter = "(&(OU=xcxc))";
   search.Filter = "(&(OU=Users))";
   search.Filter = "(&(cn=" + username + "))";
   SearchResult result = search.FindOne();

   Finally found my answer :) . Happy Coding guys :)


来源:https://stackoverflow.com/questions/55951799/fetch-active-directory-user-data-using-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!