예제 #1
0
        static void Main(string[] args)
        {
            var client = new SecureServiceClient("WSHttpBinding_ISecureService");

            //var client = new SecureServiceClient("NetTcpBinding_ISecureService");
            //var client = new InSecureServiceClient("BasicHttpBinding_IInSecureService");
            //var client = new InSecureServiceClient("NetTcpBinding_IInSecureService");

            //Remove the certificate authority validation --> dont do this in production
            ServicePointManager.ServerCertificateValidationCallback = (obj, certificate, chain, errors) => true;
            //Communicate credentials with every request to the web service
            //Unfortunate, but otherwise we would need another scheme. eg tokenbased auth
            //In your web application, you will store the credentials in memory, this is also problematic, however we will have to make do for now

            client.ClientCredentials.UserName.UserName = "******";
            client.ClientCredentials.UserName.Password = "******";

            try
            {
                client.Open();
                while (true)
                {
                    try
                    {
                        var input = Convert.ToInt32(Console.ReadLine());

                        var response = client.GetData(input);
                        // var response = client.DoSomethingInsecure(input.ToString());
                        Console.WriteLine(response);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Incorrect input.. valid values er int.MinValue to int.MaxValue");
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                if (e.InnerException != null)
                {
                    Console.WriteLine(e.InnerException.Message);
                }
            }
        }
예제 #2
0
        private static void GetDataWithValidCredentials(string username, string pw)
        {
            secureClient.ClientCredentials.UserName.UserName = username;
            secureClient.ClientCredentials.UserName.Password = pw;

            try
            {
                var data = secureClient.GetData(1337);
                Console.WriteLine("Here is some very secret information");
                Console.WriteLine(data);
            }
            catch (SecurityAccessDeniedException accessDenied)
            {
                Console.WriteLine("You do not have access to this method");
                Console.WriteLine(accessDenied.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }