Example #1
0
        public AXLPortClient CreateClient(IUcClientSettings settings)
        {
            ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
            ServicePointManager.Expect100Continue = false;
            var basicHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);

            basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

            basicHttpBinding.MaxReceivedMessageSize              = 20000000;
            basicHttpBinding.MaxBufferSize                       = 20000000;
            basicHttpBinding.MaxBufferPoolSize                   = 20000000;
            basicHttpBinding.ReaderQuotas.MaxDepth               = 32;
            basicHttpBinding.ReaderQuotas.MaxArrayLength         = 20000000;
            basicHttpBinding.ReaderQuotas.MaxStringContentLength = 20000000;
            var RosterEndpointUrl = string.Format(RosterEndpointUrlFormat, settings.Server);
            var endpointAddress   = new EndpointAddress(RosterEndpointUrl);
            var RosterClient      = new AXLPortClient(basicHttpBinding, endpointAddress);

            RosterClient.ClientCredentials.UserName.UserName = settings.User;
            RosterClient.ClientCredentials.UserName.Password = settings.Password;
            return(RosterClient);
        }
        static async Task Main(string[] args)
        {
            Console.WriteLine("\nStarting up...\n");

            // Load environment variables from .env
            DotNetEnv.Env.Load("../../.env");

            // Change to true to enable output of request/response headers and XML
            var DEBUG = System.Environment.GetEnvironmentVariable("DEBUG");

            // Create a custom binding so we can allow the client to use cookies with AXL
            BasicHttpsBinding binding = new BasicHttpsBinding();

            binding.AllowCookies = true;

            // Specify the CUCM AXL API location for the SOAP client
            EndpointAddress address = new EndpointAddress($"https://{ System.Environment.GetEnvironmentVariable( "CUCM_ADDRESS" ) }:8443/axl/");

            //Class generated from AXL WSDL
            AXLPortClient client = new AXLPortClient(binding, address);

            if (DEBUG == "True")
            {
                client.Endpoint.EndpointBehaviors.Add(new DebugEndpointBehaviour());
            }

            // To disable HTTPS certificate checking, uncomment the below lines
            // NOT for production use!  See README.md for AXL certificate install steps

            // client.ChannelFactory.Credentials.ServiceCertificate.SslCertificateAuthentication = new X509ServiceCertificateAuthentication
            //     {
            //         CertificateValidationMode = X509CertificateValidationMode.None,
            //         RevocationMode = X509RevocationMode.NoCheck
            //     };
            // client.ChannelFactory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
            // client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;

            // Incantation to force alternate serializer reflection behaviour due to complexities in the AXL schema
            // See https://github.com/dotnet/wcf/issues/2219
            MethodInfo method = typeof(XmlSerializer).GetMethod("set_Mode", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);

            method.Invoke(null, new object[] { 1 });

            // Base64 encode AXL username/password for Basic Auth
            var encodedUserPass = Convert.ToBase64String(Encoding.ASCII.GetBytes(
                                                             System.Environment.GetEnvironmentVariable("CUCM_USERNAME") + ":" +
                                                             System.Environment.GetEnvironmentVariable("CUCM_PASSWORD")
                                                             ));

            // Incantation to create and populate a Basic Auth HTTP header
            // This must be done to force SoapCore to include the Authorization header on the first attempt
            HttpRequestMessageProperty requestProperty = new HttpRequestMessageProperty();

            requestProperty.Headers["Authorization"] = "Basic " + encodedUserPass;

            // Creating a context scope allows attaching custom HTTP headers to the request
            var scope = new OperationContextScope(client.InnerChannel);

            OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestProperty;

            // Create the request object
            ExecuteSQLQueryReq request = new ExecuteSQLQueryReq();

            // Specify SQL statement we want to execute
            request.sql = "select name, pkid from applicationuser";

            object[] rows;


            executeSQLQueryResponse response = new executeSQLQueryResponse();

            //Try the getPhone request
            try
            {
                executeSQLQueryResponse response = await client.executeSQLQueryAsync(request);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"\nError: getPhone: { ex.Message }");
                Environment.Exit(-1);
            }

            Console.WriteLine($"\nexecuteSqlQuery: SUCCESS\n");

            //Parse/print the phone's model name to the console

            // Get the rows object array from the response
            rows = response.executeSQLQueryResponse1.@return;

            // Loop through each row, which consists of a XmlNode array
            foreach (XmlNode[] row in rows)
            {
                // From the first/second XmlNode in each row, parse the Value field from the FirstChild
                Console.WriteLine(
                    "Name: " + row[0].FirstChild.Value.PadRight(20) +
                    "PKID: " + row[1].FirstChild.Value
                    );
            }
        }
        static async Task Main(string[] args)
        {
            Console.WriteLine("\nStarting up...\n");

            // Load environment variables from .env
            DotNetEnv.Env.Load("../../.env");

            // Change to true to enable output of request/response headers and XML
            var DEBUG = System.Environment.GetEnvironmentVariable("DEBUG");

            // Create a custom binding so we can allow the client to use cookies with AXL
            BasicHttpsBinding binding = new BasicHttpsBinding();

            binding.AllowCookies = true;

            // Specify the CUCM AXL API location for the SOAP client
            EndpointAddress address = new EndpointAddress($"https://{ System.Environment.GetEnvironmentVariable( "CUCM_ADDRESS" ) }:8443/axl/");

            //Class generated from AXL WSDL
            AXLPortClient client = new AXLPortClient(binding, address);

            if (DEBUG == "True")
            {
                client.Endpoint.EndpointBehaviors.Add(new DebugEndpointBehaviour());
            }

            // To disable HTTPS certificate checking, uncomment the below lines
            // NOT for production use!  See README.md for AXL certificate install steps

            // client.ChannelFactory.Credentials.ServiceCertificate.SslCertificateAuthentication = new X509ServiceCertificateAuthentication
            //     {
            //         CertificateValidationMode = X509CertificateValidationMode.None,
            //         RevocationMode = X509RevocationMode.NoCheck
            //     };
            // client.ChannelFactory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
            // client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;

            // Incantation to force alternate serializer reflection behaviour due to complexities in the AXL schema
            // See https://github.com/dotnet/wcf/issues/2219
            MethodInfo method = typeof(XmlSerializer).GetMethod("set_Mode", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);

            method.Invoke(null, new object[] { 1 });

            // Base64 encode AXL username/password for Basic Auth
            var encodedUserPass = Convert.ToBase64String(Encoding.ASCII.GetBytes(
                                                             System.Environment.GetEnvironmentVariable("CUCM_USERNAME") + ":" +
                                                             System.Environment.GetEnvironmentVariable("CUCM_PASSWORD")
                                                             ));

            // Incantation to create and populate a Basic Auth HTTP header
            // This must be done to force SoapCore to include the Authorization header on the first attempt
            HttpRequestMessageProperty requestProperty = new HttpRequestMessageProperty();

            requestProperty.Headers["Authorization"] = "Basic " + encodedUserPass;

            // Creating context block allows attaching custom HTTP headers to the request
            var scope = new OperationContextScope(client.InnerChannel);

            OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestProperty;

            //Create the request object
            AddUserReq addUserReq = new AddUserReq();

            addUserReq.user          = new XUser();
            addUserReq.user.lastName = "TestUser";
            addUserReq.user.userid   = "testUser";
            addUserReq.user.password = "******";

            string userPkid = "";

            //Try the addUser request
            try
            {
                addUserResponse addUserResp = await client.addUserAsync(addUserReq);

                userPkid = addUserResp.addUserResponse1.@return;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"\nError: addUser: { ex.Message }");
                Environment.Exit(-1);
            }

            Console.WriteLine($"addUser: SUCCESS  pkid: { userPkid }\n");

            Console.WriteLine("Press Enter to continue...");
            Console.ReadLine();

            RemoveUserReq removeUserReq = new RemoveUserReq();

            removeUserReq.ItemElementName = ItemChoiceType102.userid;
            removeUserReq.Item            = "testUser";

            //Try the removeUser request
            try
            {
                removeUserResponse removeUserResp = await client.removeUserAsync(removeUserReq);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"\nError: removeUser: { ex.Message }");
                Environment.Exit(-1);
            }

            Console.WriteLine("removeUser: SUCCESS");
        }