Quick Start (MTOM): Managing roles and permissions using the web service API

The following C# .NET code example assigns a role to a principal, prints the roles the principal has, and subsequently removes the role from the principal. Two services are invoked for this quick start. The AssignRoles.ServiceReference1 namespace references the DirectoryManager service. The AssignRoles.ServiceReference2 namespace references the AuthorizationManager service. (See Managing Roles and Permissions.)

/** 
    * Ensure that you create a .NET project that uses  
    * MS Visual Studio 2008 and version 3.5 of the .NET 
    * framework. This is required to invoke a  
    * LiveCycle service using MTOM. 
    *      
    * For information, see "Invoking LiveCycle using MTOM" in Programming with LiveCycle   
    */ 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ServiceModel; 
using System.IO; 
 
//A reference to the DirectoryManager service 
using AssignRoles.ServiceReference1; 
 
//A reference to the AuthorizationManager service 
using AssignRoles.ServiceReference2; 
 
namespace AssignRoles 
{ 
       class Program 
       { 
           static void Main(string[] args) 
           { 
               try 
               { 
                   //Create a DirectoryManagerServiceClient object 
                   DirectoryManagerServiceClient dirManClient = new DirectoryManagerServiceClient(); 
                   dirManClient.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://hiro-xp:8080/soap/services/DirectoryManagerService?blob=mtom"); 
 
                   //Enable BASIC HTTP authentication 
                   BasicHttpBinding b = (BasicHttpBinding)dirManClient.Endpoint.Binding; 
                   b.MessageEncoding = WSMessageEncoding.Mtom; 
                   dirManClient.ClientCredentials.UserName.UserName = "administrator"; 
                   dirManClient.ClientCredentials.UserName.Password = "password"; 
                   b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; 
                   b.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; 
                   b.MaxReceivedMessageSize = 2000000; 
                   b.MaxBufferSize = 2000000; 
                   b.ReaderQuotas.MaxArrayLength = 2000000; 
 
                    //Create an AuthorizationManagerServiceClient object 
                   AuthorizationManagerServiceClient amClient  = new AuthorizationManagerServiceClient(); 
                   amClient.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://hiro-xp:8080/soap/services/AuthorizationManagerService?blob=mtom"); 
 
                   //Enable BASIC HTTP authentication 
                   BasicHttpBinding b1 = (BasicHttpBinding)amClient.Endpoint.Binding; 
                   b1.MessageEncoding = WSMessageEncoding.Mtom; 
                   amClient.ClientCredentials.UserName.UserName = "administrator"; 
                   amClient.ClientCredentials.UserName.Password = "password"; 
                   b1.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; 
                   b1.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; 
                   b1.MaxReceivedMessageSize = 2000000; 
                   b1.MaxBufferSize = 2000000; 
                   b1.ReaderQuotas.MaxArrayLength = 2000000; 
     
                   //Create a PrincipalSearchFilter to find the user to delete 
                   AssignRoles.ServiceReference1.PrincipalSearchFilter psf = new AssignRoles.ServiceReference1.PrincipalSearchFilter(); 
                   psf.userId = "wblue"; 
                   MyArrayOfUser allUsers = dirManClient.findUsers(psf); 
                   string oid = ""; 
 
                   //Determine how many elements there are 
                   //Each element is of type User 
                   int index = allUsers.Count; 
 
                   if (index == 1) 
                   { 
                       //Iterate through the array  
                       for (int i = 0; i < index; i++) 
                       { 
                           // Obtain the principals object identifier 
                           AssignRoles.ServiceReference1.User theUser = (AssignRoles.ServiceReference1.User)allUsers[i]; 
                           AssignRoles.ServiceReference2.MyArrayOf_xsd_string principalOids = new AssignRoles.ServiceReference2.MyArrayOf_xsd_string(); 
                           oid = theUser.oid; 
                           principalOids.Add(oid); 
 
                           //Obtain the roles to be assigned 
                           AssignRoles.ServiceReference2.RoleSearchFilter rsf = new AssignRoles.ServiceReference2.RoleSearchFilter(); 
                           rsf.roleName = "Services User"; 
                           AssignRoles.ServiceReference2.MyArrayOfRole roleList = amClient.findRoles(rsf); 
                           string roleId1 = ""; 
                           if (roleList.Count > 0) 
                           { 
                               // Obtain the role identifier 
                               AssignRoles.ServiceReference2.Role testRole = (AssignRoles.ServiceReference2.Role)roleList[0]; 
                               roleId1 = testRole.id; 
 
                               // Assign the role to the principal 
                               amClient.assignRole(roleId1, principalOids); 
                           } 
                           else 
                           { 
                               Console.WriteLine("Role not found"); 
                           } 
 
                           // Determine which roles the principal has 
                           MyArrayOfRole roleSet = amClient.findRolesForPrincipal(oid); 
 
                           // Print the roles the principal has 
                           AssignRoles.ServiceReference2.Role r = null; 
                           Console.WriteLine("Roles:"); 
                           for (int z = 0; z < roleSet.Count; z++) 
                           { 
                               r = (AssignRoles.ServiceReference2.Role)(roleSet[z]); 
                               Console.WriteLine(r.name); 
                           } 
 
                           // Remove a role from the principal 
                           amClient.unassignRoleForPrincipals(roleId1, principalOids); 
                       }//end of for loop 
                   } 
 
                   else 
                   { 
                       Console.WriteLine("Principal not found"); 
                   } 
               } 
               catch (Exception ee) 
               { 
                   Console.WriteLine("An unexpected exception was encountered: " + ee.Message + "\n" + ee.StackTrace); 
               } 
           } 
       } 
}