locked
Communicating with the Active Directory RRS feed

  • Question

  • User-1781589230 posted

    Hello

    I'm having a form here that I want to be populated with some user infos from the Active Directory, and I have another form also for my application users to provide new data of a user and then click (ADD) to add this new user to the Active Directory.

    So, I would like to know how I can read/write user infos from/to an Active Directory? Is there a ready-made code that can do such  operations?

    Monday, April 16, 2007 11:19 PM

Answers

  • User-847590259 posted

    Hi,

    This function is used for getting information about a user. To get the display name of a user you have to put in "inType" the value "displayname".

     

    private static string GetUserInfoByLogin( string login, string inType) 
    		{
    			try
    			{
    				// Cogemos la cadena de conexión del web.config
    				string sPath = ConfigurationSettings.AppSettings["ADServer"];
    				// Dim SamAccount As String = Right(inSAM, Len(inSAM) - InStr(inSAM, "\"))
    				string SamAccount = login;
    				// Usuario y Password del que accede al AD
    				DirectoryEntry myDirectory = new DirectoryEntry(sPath, "dausr", "") ;
    
    				// Dim myDirectory As New DirectoryEntry(sPath)
    				DirectorySearcher mySearcher = new DirectorySearcher( myDirectory );
    				SearchResultCollection mySearchResultColl = null;
    				SearchResult mySearchResult = null;
    				ResultPropertyCollection myResultPropColl = null;
    				ResultPropertyValueCollection myResultPropValueColl = null;
     
    				// Construimos la consulta LDAP
    				// mySearcher.Filter = ("(&(objectClass=user)(samaccountname=" & SamAccount & "))")
    				mySearcher.Filter = "(SAMAccountName=" + SamAccount + ")";
     
    				mySearchResultColl = mySearcher.FindAll();
    				
    				// Solamente esperamos un resultado
    				if ( 0 == mySearchResultColl.Count || 1 < mySearchResultColl.Count )
    					return null;
    
    				// Metemos la busqueda en la coleccion
    				mySearchResult = mySearchResultColl[0];
    
    				// Pillamos las propiedades que contienen la información util
    				myResultPropColl = mySearchResult.Properties;
    
    				myResultPropValueColl = myResultPropColl[inType];
    				if ( null == myResultPropValueColl )
    					return "";
    				else
    					return Convert.ToString(myResultPropValueColl[0]);
    
    			}
    			catch( Exception ex )
    			{
    				throw new Exception( "Función: GetUserInfoByLogin()." + ex.Message, ex );
    			}
    		}
      
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 17, 2007 5:33 AM

All replies

  • User-847590259 posted

    Hi,

    This function is used for getting information about a user. To get the display name of a user you have to put in "inType" the value "displayname".

     

    private static string GetUserInfoByLogin( string login, string inType) 
    		{
    			try
    			{
    				// Cogemos la cadena de conexión del web.config
    				string sPath = ConfigurationSettings.AppSettings["ADServer"];
    				// Dim SamAccount As String = Right(inSAM, Len(inSAM) - InStr(inSAM, "\"))
    				string SamAccount = login;
    				// Usuario y Password del que accede al AD
    				DirectoryEntry myDirectory = new DirectoryEntry(sPath, "dausr", "") ;
    
    				// Dim myDirectory As New DirectoryEntry(sPath)
    				DirectorySearcher mySearcher = new DirectorySearcher( myDirectory );
    				SearchResultCollection mySearchResultColl = null;
    				SearchResult mySearchResult = null;
    				ResultPropertyCollection myResultPropColl = null;
    				ResultPropertyValueCollection myResultPropValueColl = null;
     
    				// Construimos la consulta LDAP
    				// mySearcher.Filter = ("(&(objectClass=user)(samaccountname=" & SamAccount & "))")
    				mySearcher.Filter = "(SAMAccountName=" + SamAccount + ")";
     
    				mySearchResultColl = mySearcher.FindAll();
    				
    				// Solamente esperamos un resultado
    				if ( 0 == mySearchResultColl.Count || 1 < mySearchResultColl.Count )
    					return null;
    
    				// Metemos la busqueda en la coleccion
    				mySearchResult = mySearchResultColl[0];
    
    				// Pillamos las propiedades que contienen la información util
    				myResultPropColl = mySearchResult.Properties;
    
    				myResultPropValueColl = myResultPropColl[inType];
    				if ( null == myResultPropValueColl )
    					return "";
    				else
    					return Convert.ToString(myResultPropValueColl[0]);
    
    			}
    			catch( Exception ex )
    			{
    				throw new Exception( "Función: GetUserInfoByLogin()." + ex.Message, ex );
    			}
    		}
      
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 17, 2007 5:33 AM
  • User-1781589230 posted
    Thanks a lot, I appreciate your help :)
    Tuesday, April 17, 2007 10:41 PM