Principal Search Example

The following code example illustrates how to perform a search for a group or user in a realm. This example assumes a web reference named CEWS_DIME has been set to the following URI: http://localhost:<port>/wsi/FNCEWS35DIME/wsdl.

using System;
using Microsoft.Web.Services2;
using Microsoft.Web.Services2.Security.Tokens;
using principalsearch_ex.CEWS_DIME;

namespace principalsearch_ex
{
   /// <summary>
   /// Example principal search: get the principal name for a given short name.
   /// </summary>
   class Class1
   {
      /// <summary>
      /// The main entry point for the application.
      /// </summary>
      [STAThread]
      static void Main(string[] args)
      {
         const string url = "http://localhost:9080/wsi/FNCEWS35DIME/"; // Change port number if necessary
         const string realm = "DC=seattle,DC=local"; // The realm in which to search
         string shortName = "Administrator"; // Short name of an existing user
   
         // Create a wse-enabled web service object to provide access to SOAP header
         FNCEWS35ServiceWse wseService = new FNCEWS35ServiceWse();
         wseService.Url = url;
         SoapContext soapContext = wseService.RequestSoapContext;
		 
         // Add security token to SOAP header with your username and password
         UsernameToken token = new UsernameToken("username", "password", PasswordOption.SendPlainText);
         soapContext.Security.Tokens.Add(token);
		 
         // Add default locale info to SOAP header
         Localization defaultLocale = new Localization();
         defaultLocale.Locale = "en-US";
   
         // Create PrincipalSearch
         PrincipalSearch elemPrincipalSearch = new PrincipalSearch();
         elemPrincipalSearch.SearchRealm = realm;
         elemPrincipalSearch.SearchPattern = shortName;
         elemPrincipalSearch.includeUsers = true;
         elemPrincipalSearch.includeUsersSpecified = true;
         elemPrincipalSearch.includeGroups = true;
         elemPrincipalSearch.includeGroupsSpecified = true;
         elemPrincipalSearch.principalSearchType = PrincipalSearchPrincipalSearchType.Exact;
         elemPrincipalSearch.principalSearchTypeSpecified = true;
         elemPrincipalSearch.principalSearchAttribute = PrincipalSearchPrincipalSearchAttribute.ShortName;
         elemPrincipalSearch.principalSearchAttributeSpecified = true;
   
         //Construct Property Filter for Name property
         PropertyFilterType elemPropertyFilterType = new PropertyFilterType();
         elemPropertyFilterType.maxRecursion = 0;
         elemPropertyFilterType.maxRecursionSpecified = true;
         elemPropertyFilterType.IncludeProperties = new FilterElementType[1];
         elemPropertyFilterType.IncludeProperties[0] = new FilterElementType();
         elemPropertyFilterType.IncludeProperties[0].Value = "Name";
         elemPrincipalSearch.SelectionFilter = elemPropertyFilterType;
		 
         // Execute Search
         ObjectSetType objObjectSet = wseService.ExecuteSearch(elemPrincipalSearch);
   
         // Verify it was found
         if (objObjectSet == null)
         {
            Console.WriteLine("User or Group with ShortName '{0}' was not found in Realm '{1}'.", shortName, realm);
         }
   
         // Verify only one object was found
         if (objObjectSet.Object.Length > 1) 
         {
            Console.WriteLine("More than one User or Group with ShortName '{0}' was found in Realm '{1}'.", shortName, realm);
         }
		 
         // Get Name property 
         SingletonString propName = (SingletonString) objObjectSet.Object[0].Property[0];
         Console.WriteLine("Principal Name of '{0}' is '{1}'.", shortName, propName.Value);
         Console.WriteLine("Press Enter to end");
         Console.ReadLine();		 
         
      } //Main
   } // class1
}