locked
Reading LDAP from ASP.NET RRS feed

  • Question

  • User-1915511747 posted

    Hi

    How can we read local windows directory by ASP.NET web application?

    Regards,

    Afreen


    Thursday, January 28, 2010 2:36 AM

Answers

  • User-725797108 posted

    You need to add this library (System.DirectoryServices) as refrence to your project first.

    After adding as reference, just Import this library in your page and you should be able to find directoryentry.

     

    dim employee As DirectoryEntry

     

    Let me know if this solves your query and don't forget to mark as Answer if this is correct.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 29, 2010 12:01 AM

All replies

  • User-68639941 posted

    hi, refer below code

    private static DirectoryEntry GetDirectoryObject()   

  •    {   
  •        DirectoryEntry directoryEntry = default(DirectoryEntry);   
  •        directoryEntry = new DirectoryEntry("LDAP://172.231.100.232/DC=test,DC=COM""user1""password1", AuthenticationTypes.Secure);   
  •        return directoryEntry;   
  •    }   
  •   
  •    public static string GetUserName(string UserName)   
  •    {   
  •        //create an instance of the DirectoryEntry    
  •        DirectoryEntry de = GetDirectoryObject();   
  •   
  •        //create instance fo the direcory searcher    
  •        DirectorySearcher deSearch = new DirectorySearcher(de);   
  •   
  •        //set the search filter    
  •        deSearch.SearchRoot = de;   
  •        deSearch.Filter = "(&(objectCategory=user)(samaccountname=" + UserName + "))";   
  •   
  •        //find the first instance    
  •        SearchResultCollection results = deSearch.FindAll();   
  •        string val = string.Empty;   
  •        if (results.Count > 0)   
  •        {   
  •            SearchResult sr = results[0];   
  •            ResultPropertyValueCollection valColl = sr.Properties["employeeNumber"];   
  •            val = valColl[0].ToString();   
  •        }   
  •   
  •        if (string.IsNullOrEmpty(val))   
  •        {   
  •            return "0";   
  •        }   
  •        else  
  •        {   
  •            return val;   
  •        }   
  •    }  
    e static DirectoryEntry GetDirectoryObject()
        {
            DirectoryEntry directoryEntry = default(DirectoryEntry);
            directoryEntry = new DirectoryEntry("LDAP://172.231.100.232/DC=test,DC=COM", "user1", "password1", AuthenticationTypes.Secure);
            return directoryEntry;
        }
    
        public static string GetUserName(string UserName)
        {
            //create an instance of the DirectoryEntry 
            DirectoryEntry de = GetDirectoryObject();
    
            //create instance fo the direcory searcher 
            DirectorySearcher deSearch = new DirectorySearcher(de);
    
            //set the search filter 
            deSearch.SearchRoot = de;
            deSearch.Filter = "(&(objectCategory=user)(samaccountname=" + UserName + "))";
    
            //find the first instance 
            SearchResultCollection results = deSearch.FindAll();
            string val = string.Empty;
            if (results.Count > 0)
            {
                SearchResult sr = results[0];
                ResultPropertyValueCollection valColl = sr.Properties["employeeNumber"];
                val = valColl[0].ToString();
            }
    
            if (string.IsNullOrEmpty(val))
            {
                return "0";
            }
            else
            {
                return val;
            }
        }
    

     

    Refer : http://www.c-sharpcorner.com/uploadfile/ecabral/adand.net08242005065451am/adand.net.aspx

    http://www.c-sharpcorner.com/Articles/ArticleListing.aspx?SectionID=1&SubSectionID=1

    http://www.devx.com/dotnet/Article/33395/0/page/2

    to get more just google

     

    add reference and define using statment of "using System.DirectoryServices;"


    right-click your web application project in visual studio, it pop-up a window from that you can choose "System.DirectoryServices" from .Net tab

    else add below line in web.config file but dont duplicate the these line

     <compilation debug="true">
       <assemblies>
        <add assembly="System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
        
       </assemblies>
      </compilation>

     

Thursday, January 28, 2010 3:08 AM
  • User-1915511747 posted

    Hi,

    Thanks for the prompt reply.

    Can you please also tell me in which library I can find DirectoryEntry class?


    Regards,


    Afreen




    Thursday, January 28, 2010 3:49 AM
  • User-1915511747 posted

    Thanks again.

    I am not able to find this library (using System.DirectoryServices; ) in my C# Web Application.

    Do I need to download something?


    Regards,


    Afreen


    Thursday, January 28, 2010 4:04 AM
  • User-725797108 posted

    You need to add this library (System.DirectoryServices) as refrence to your project first.

    After adding as reference, just Import this library in your page and you should be able to find directoryentry.

     

    dim employee As DirectoryEntry

     

    Let me know if this solves your query and don't forget to mark as Answer if this is correct.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 29, 2010 12:01 AM