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 has been set to the following URI: http://localhost:<port>/wsi/FNCEWS40MTOM/wsdl.

using System;      
using Microsoft.Web.Services3;
using Microsoft.Web.Services3.Security.Tokens;
using Microsoft.Web.Services3.Design;
using principalsearch_ex.CEWS;

namespace principalsearch_ex
{
   /// <summary>
   /// Principal search that retrieves the principal name for a given short name.
   /// </summary>
   class Class1
   {
      static void Main(string[] args)
      {
         const string strUrl = "http://localhost:9080/wsi/FNCEWS40MTOM/"; // Change port number if necessary
         const string objRealm = "DC=seattle,DC=local"; // The realm in which to search
         string strShortName = "Administrator"; // Short name of an existing user
   
         // Create a wse-enabled web service object
         FNCEWS40ServiceWse wseService = new FNCEWS40ServiceWse();
		 
         // Set security token with your username and password
         UsernameToken token = new UsernameToken("username", "password", PasswordOption.SendPlainText);
         wseService.SetClientCredential(token);
         Policy MyPolicy = new Policy();
         MyPolicy.Assertions.Add(new UsernameOverTransportAssertion());
         wseService.SetPolicy(MyPolicy);
         wseService.Url = strUrl;
         wseService.RequireMtom = true;

         // Add default locale info to SOAP header
         Localization objDefaultLocale = new Localization();
         objDefaultLocale.Locale = "en-US";
   
         // Create PrincipalSearch
         PrincipalSearch objPrincipalSearch = new PrincipalSearch();
         objPrincipalSearch.SearchRealm = objRealm;
         objPrincipalSearch.SearchPattern = strShortName;
         objPrincipalSearch.includeUsers = true;
         objPrincipalSearch.includeUsersSpecified = true;
         objPrincipalSearch.includeGroups = true;
         objPrincipalSearch.includeGroupsSpecified = true;
         objPrincipalSearch.principalSearchType = PrincipalSearchPrincipalSearchType.Exact;
         objPrincipalSearch.principalSearchTypeSpecified = true;
         objPrincipalSearch.principalSearchAttribute = PrincipalSearchPrincipalSearchAttribute.ShortName;
         objPrincipalSearch.principalSearchAttributeSpecified = true;
   
         //Construct Property Filter for Name property
         PropertyFilterType objPropertyFilterType = new PropertyFilterType();
         objPropertyFilterType.maxRecursion = 0;
         objPropertyFilterType.maxRecursionSpecified = true;
         objPropertyFilterType.IncludeProperties = new FilterElementType[1];
         objPropertyFilterType.IncludeProperties[0] = new FilterElementType();
         objPropertyFilterType.IncludeProperties[0].Value = "Name";
         objPrincipalSearch.SelectionFilter = objPropertyFilterType;
		 
         // Execute Search
         ObjectSetType objObjectSet = wseService.ExecuteSearch(objPrincipalSearch);
   
         // Verify it was found
         if (objObjectSet == null)
         {
            Console.WriteLine("User or Group with ShortName '{0}' was not found in Realm '{1}'.", strShortName, objRealm);
         }
   
         // 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}'.", strShortName, objRealm);
         }
		 
         // Get Name property 
         SingletonString prpName = (SingletonString) objObjectSet.Object[0].Property[0];
         Console.WriteLine("Principal Name of '{0}' is '{1}'.", strShortName, prpName.Value);
         Console.WriteLine("Press Enter to end");
         Console.ReadLine();		 
      }
   }
}