This is a discussion on Visual Studio Tips & Tricks within the C# Programming forums, part of the Software Development category; Building an LDAP Web Service Download the Source Code This is a three part article on building an LDAP web ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Building an LDAP Web Service Download the Source Code This is a three part article on building an LDAP web service. LDAP, What is Lightweight Directory Access Protocol Reading Information from the LDAP Server Converting LDAP to Web Service Okay, now we are ready to create our web service and web methods that call this class and method. Open the code behind on the LDAPWebServ.asmx file. Create a new WebMethod. [WebMethod] public XmlDocument GetAllUsers() { string filter = "samaccountname=*"; LDAPComLink lcl = new LDAPComLink(); XmlDocument xd = new XmlDocument(); xd = lcl.GetLDAPInfo(filter); return xd; } As you can see, if you create your class correctly, there isn't much to creating the WebMethod. First we create the filter we want to use. The method we are creating is GetAllUsers. We want to bring back all users on the Directory Server. The filter needed for this step will be dependent on your specific LDAP object. For the Sun One LDAP I used "uid=*". But as you can see, the Active Directory required the filter "samaccountname=*". You have to see your data and then determine at what level to filter. Then we create an instance of the LDAPComLink object, the class we built earlier. Create an instance of an XmlDocument and then call our GetLDAPInfo method, pass in the filter information and capture the XmlDocument object that is returned. Then the method itself returns the XmlDocument to the caller. If you don't do anything more than what we have gotten up to this point you should be able to call your method and return the user information from the LDAP server. Set LDAPWebServ.asmx as start page and then run your web service. Choose the link "GetAllUsers" and it should flash up a page with an Invoke button. Click the button and if we did everything right we should see our XML of users and other properties returned from the LDAP application. To make this web service more robust, you can send the configuration and connectivity information with the filter to the class. You can also add a method for authenticating a user and another method for returning all the groups. All this information is available in the LDAP server, you just need to know how to query it for the right information. In the code that is provided with this article, I have added additional queries you can send to the Directory server. Hopefully, you will find them useful. When accessing an LDAP directory server, you need to build a DirectoryEntry and DirectorySearcher objects. Place the results in a SearchResultCollection and then filter on the Collection. To pick out individual pieces from the LDAP, just iterate through the collection pulling out properties and their values. If you place all the work of querying the LDAP server and the connectivity issues in another class, then the WebMethods from the WebService are simple to create and clutter free. If you have multiple LDAP servers to access, then create a configuration file and get the connectivity information from the configuration file. This article presented how to access an LDAP directory, the C#.Net way to read the information in the LDAP server and then how to use XmlDocument objects to present them to a Web Service. And finally, how to create the Web Service using the [WebMethod] keyword. Happy Programming. |
| Sponsored Links |
| |||
| Visual Studio features a number of specialized editors—Binary Editor, CSS Source Editor, HMTL Editor, Resource Editor, Source Code Editor, XML Editor, etc. But, you may have a personal favorite editor for a particular type of file. For example, Visual Studio 2005 offers a CSS Source Editor for editing stylesheets (.css). But, you might prefer the TopStyle editor. There is a way that you can use it instead of the default editor. Create a file Navigate the View / Solution Explorer menus to open the Solution Explorer. Right click on a project and select Add / New Item… from the popup-up menu. This opens the Add New Item Dialog Box: Figure: Visual Studio Add New Item Dialog Box Click on the file type you wish to create. In this case, the Style Sheet file type is selected. Type the filename in the Name text box; and, click the Add button. This creates a new file in your project. In this case, the file is named Stylesheet1.css. |
| |||
| Add the new editor To add your favorite editor for this file type, right click the new file in the Solution Explorer. Select Open With… from the pop-up menu. This will bring up the Open With… dialog box: ![]() Figure: Visual Studio Open With… Dialog Box Now, click the Add… button. This brings up a second dialog box like this: ![]() Figure: Visual Studio Add Program Dialog Box Type the location of your favoite editor into the Program name text box; or, use the browse button (…) to find it. Then, type the name of your editor in the Friendly name text box. Click the OK button to add it to the list of available editors. |
| |||
| Set the default editor Now, you can either set your favorite editor as the default for this type of file using the Set as Default button; or, you can use the Open With… command each time you wish to use it. ![]() Figure: Visual Studio Open With… Dialog Box |
| |||
| The AutoRecover feature was introduced with Visual Studio 2005. AutoRecover allows you to configure Visual Studio to save your projects and solutions automatically. So, your work can be recovered in the event of a crash. Configure AutoRecover First, navigate through the Tool / Options… menus to the Options dialog box. Then, select Environment / AutoRecover in the left panel of the Options dialog box. Figure: Visual Studio Options Dialog Box Be sure the Save AutoRecover information every: checkbox is checked. Now, enter the number of minutes between automatic backups. If you enter 10, Visual Studio will automatically create a backup every ten minutes. Next, enter the number of days you wish to keep previous backups in the Keep AutoRecover information for: control. If you enter 10, Visual Studio will retain previous backups for ten days. Finally, click the OK button. If Visual Studio or the system should crash, then the next time you start up Visual Studio it will prompt you to have Visual Studio automatically restore your project or solution. |
| |||
| If you work in Visual Studio on multiple computers or if you share Visual Studio with someone else on the same computer, it is very handy to be able to import your personal Visual Studio settings. It is very disconcerting to work without your personal source code markup, custom keyboard shortcuts, and other personal settings. And, it is nice to have a backup, just in case Visual Studio or an update should lose your personal settings. Export personal settings Navigate to the Tools / Import and Export Settings… menus to access the Import and Export Settings Wizard. Figure 1. Import and Export Settings Wizard Be sure Export selected environment settings is checked. Then, click the Next button; and, the following screen will be displayed: Figure 2. Select settings for export Check off all the settings you wish to export. Then, click the Next button. In the next step, you will provide the name and location for the exported settings. Figure 3. Enter a name and directory for the exported settings After entrering the name and location of your settings, click the Finish button. The wizard will export your selected settings. Now, your personal Visual Studio settings are in a file which you can move to another computer and Import. |
| |||
| Import personal settings Navigate the Tools / Import and Export Settings… menus to the Import and Export Settings Wizard. Figure 4. Import and Export Settings Wizard Check Import selected environment settings. Then, click the Next button; and, the following screen will be displayed: Figure 5. Option to save the current settings If you wish to save the current Visual Studio settings, check the Yes radio button and enter the name and directory for the settings to be saved. Otherwise, check No. In any case, click the Next button. Next, you select the settings you wish to import. In this example, the previously exported settings file named Exported-2006-10-23.vssettings is selected. Figure 6. Select the settings collection to import Simply, select the individual settings you wish to import from the settings collection on the following screen: Figure 7. Select indiviudal settings to import Finally, click the Finish button; and, Visual Studio will import the settings. |
| |||
| An interface is implemented at class definition level by using the : (colon) operator after the class name but before the interface name. Then, you must provide concrete implementations of the methods of that interface. In Visual Studio, this task is automated for you; so, you do not have to type out all the methods. Steps:
![]() Figure: Implementing an interface in Visual Studio |
| |||
| An external tool can be launched from the Visual Studio Tool menu. A few external tools are pre-installed in Visual Studio, e.g. Create GUID. But, you can add external tools of your own choice. External Tools dialog box To access the External Tools… dialog box, navigate the Tools / External Tools… menus. ![]() Figure: Visual Studio External Tools Dialog Box From this dialog, you can add or delete external tools. And, you can edit the Arguments and Initial directory for a tool. Also, you can reorder the list of tool names which is in the order it will appear in the Tool menu. To add your external tool, click the Add button of the External Tools dialog box. Change the Title text box to the tool name you want to appear in the Tool menu. Enter the path to the tool in the Command text box. Or, use the arrow button next to it to browse to the tool. Finally, enter the Initial directory path; or, select one of the choices using the arrow button. |
| |||
| Arguments for external tools You can choose to pass these predefined arguments to your external tool. To access them, click the arrow button to the right of the Arguments text box. Name Argument Description Item Path $(ItemPath) The complete file name of the current source (defined as drive + path + file name); blank if a non-source window is active. Item Directory $(ItemDir) The directory of the current source (defined as drive + path); blank if a non-source window is active. Item File Name $(ItemFilename) The file name of the current source (defined as file name); blank if a non-source window is active. Item Extension $(ItemExt) The file name extension of the current source. Current Line $(CurLine) The current line position of the cursor in the editor. Current Column $(CurCol) The current column position of the cursor in the editor. Current Text $(CurText) The selected text. Target Path $(TargetPath) The complete file name of the item to be built, (defined as drive + path + file name). Target Directory $(TargetDir) The directory of the item to be built. Target Name $(TargetName) The file name of the item to be built. Target Extension $(TargetExt) The file name extension of the item to be built. Project Directory $(ProjDir) The directory of the current project (defined as drive + path). Project file name $(ProjFileName) The file name of the current project (defined as drive + path + file name). Solution Directory $(SolutionDir) The directory of the current solution (defined as drive + path). Solution file name $(SolutionFileName) The file name of the current solution (defined as drive + path + file name). Last edited by rrrajesh84in : 12-16-2007 at 09:58 PM. |
| |||
| Close on exit check box If the Close on exit check box is checked, the command prompt window will close when the external tool exits. Normally, this is checked. However, if you need to see the output in the command prompt window but it closes too fast to read, uncheck this. |
| |||
| Prompt for arguments check box If the Prompt for arguments check box is checked, the Arguments dialog box will appear to specify new arguments or to edit existing arguments for the external tool. This will occur each time the tool is executed. Arguments:- Lists the currently specified variables for the tool in the External Tools dialog box. For a full list of the predefined arguments, refer to Arguments for External Tools. A custom argument can also be entered. Command line:- Displays the value for the arguments specified in the Arguments box. |
| |||
| Treat output as Unicode check box If you use an external tool that generates Unicode output, you may opt to display the Unicode output in the Output window by checking this box. Use Output window check box If the Use Output window check box is checked, output generated by the command to the external tool will appear in the Visual Studio Output window rather than in a command prompt window. Valid only for for .bat and .com files. |
| |||
| You have just run your first console application in Visual Studio debug mode. But, the window closes before you can see the results! Start without debugging The easiest way of avoiding the problem is to start your console application from Visual Studio without using debugging. To do this, select Start Without Debugging from the Debug menu (CTRL-F5). This time when the program ends execution, the window will remain open until you press any key on the keyboard. |
| |||
| Wait for console input Or, you can add this line of C# code at the end your application: Console.ReadKey(); This will cause you application to wait until a key is pressed on the keyboard; and, then exit. |
| |||
| Snap lines were introduced with Visual Studio 2005. They are used to faciliate the positioning of elements on Windows Forms. But, snap lines can be disabled if you prefer the snap to grid layout style of Visual Studio 2003. Change Visual Studio Options The grid layout settings are found on the Visual Studio 2005 Options dialog box. To access the Options dialog box, select the Tools / Options… menus. It is the same in the Visual Studio Express Editions. |
| |||
| Check Show all settings If you do not see the Windows Forms Designer item in the left panel of the Options dialog box—only Environment, Text Editor, and Debugging are viewable—, look for the Show all settings checkbox at the bottom left of the Options dialog. Check it to make the other settings viewable. Select SnapToGrid
Figure: Visual Studio Layout Settings |
| |||
| Many development tools—e.g. FxCop—refer to source code line numbers. Visual Studio .NET always shows the current line number, but finding a particular line called out by a tool is simpler if source code line numbering is enabled. Change Visual Studio Options The grid layout settings are found on the Visual Studio 2005 Options dialog box. To access the Options dialog box, select the Tools / Options… menus. It is the same in the Visual Studio Express Editions. Check Line numbers
|