public void CallService(string message, string errorMessage)
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            AccessToken accessToken = (AccessToken)accessTokenCache.Get(ConfigVariables.AccessTokenServiceUrl);
            string      responseData;

            if (accessToken != null)
            {
                Console.WriteLine("AccessToken received from Cache: " + accessToken.Value);
                responseData = SendRequest(accessToken, message, errorMessage);
                Console.WriteLine("ResponseData:\n" + responseData);

                return;
            }

            SecurityToken samltoken = (SecurityToken)samlTokenCache.Get(ConfigVariables.DemoServiceUrl);

            if (samltoken == null)
            {
                // Get SAML token


                samltoken = (SecurityToken)TokenFetcher.IssueSamlToken(ConfigVariables.ServiceEntityId);
                samlTokenCache.Add(new CacheItem(ConfigVariables.DemoServiceUrl, samltoken),
                                   new CacheItemPolicy {
                    AbsoluteExpiration = samltoken.ValidTo.ToUniversalTime()
                });
                //print all cache key value again to check updates
                Console.WriteLine("All key-values after updates of samlTokenCache ");
                PrintAllCache(samlTokenCache);
            }

            Console.WriteLine("SamlTokenId: " + samltoken.Id);
            // Convert to Access Token
            accessToken = TokenFetcher.ConvertToAccessToken(samltoken);
            Console.WriteLine("AccessToken received from Service: " + accessToken.Value);
            accessTokenCache.Add(new CacheItem(ConfigVariables.AccessTokenServiceUrl, accessToken),
                                 new CacheItemPolicy {
                AbsoluteExpiration = DateTime.UtcNow + accessToken.ExpiresIn
            });
            //print all cache key value again to check updates
            Console.WriteLine("All key-values after updates of accessTokenCache ");
            PrintAllCache(accessTokenCache);
            // Call REST service
            responseData = SendRequest(accessToken, message, errorMessage);
            Console.WriteLine("ResponseData:\n" + responseData);
        }
        public string CallDemoServiceWithToken(string message, string endpointUrl)
        {
            // Security protocols supported by the DemoService
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            var token = TokenFetcher.IssueToken(ConfigVariables.ServiceEntityId);
            callDemoServiceRequest request = new callDemoServiceRequest
            {
                CallDemoServiceRequest1 = new CallDemoServiceRequestType
                {
                    messageString = message,
                    CallContext   = GetCallContext()
                }
            };
            DemoPortType channel = CreateChannel(token, endpointUrl);

            // Disable server certificate check when using self-signed certificate (do not use in production).
            // Should be uncommented if you intent to call DemoService locally.
            // ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) => true;

            callDemoServiceResponse response = channel.callDemoService(request);

            return(response.CallDemoServiceResponse1.responseString);
        }
        private string SendRequest(AccessToken token, string message, string errorMessage)
        {
            String     demoServiceURL = ConfigVariables.DemoServiceUrl + message;
            UriBuilder demoServiceURI = new UriBuilder(demoServiceURL);

            if (errorMessage != null && !errorMessage.Equals(""))
            {
                string queryToAppend = HttpUtility.UrlEncode("errogrMessage=") + HttpUtility.UrlEncode(errorMessage);
                demoServiceURI.Query = queryToAppend;
            }

            try {
                HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(demoServiceURI.Uri);
                webrequest.Method      = "GET";
                webrequest.ContentType = "application/json";
                TokenFetcher.AddClientCertificate(ref webrequest);
                webrequest.Headers.Add("Authorization", "Holder-of-key " + token.Value);
                string transaktionsId = Guid.NewGuid().ToString();
                webrequest.Headers.Add("x-TransaktionsId", transaktionsId);
                string transaktionsTid = DateTime.UtcNow.ToString(DateTimeFormat, CultureInfo.InvariantCulture);
                webrequest.Headers.Add("x-TransaktionsTid", transaktionsTid);

                Console.WriteLine("----------");
                Console.WriteLine("Request URL: " + demoServiceURI.Uri.OriginalString);
                Console.WriteLine("Request Headers: " + webrequest.Headers);
                Console.WriteLine("----------");

                WebResponse response = webrequest.GetResponse();
                Console.WriteLine("**********Response From Demo Service******************************");
                Console.WriteLine("\nContentType: " + response.ContentType);
                Console.WriteLine("ContentLength: " + response.ContentLength);
                Console.WriteLine("Headers: " + response.Headers);
                Console.WriteLine("Type: " + response.GetType());
                Console.WriteLine("IsFromCache: " + response.IsFromCache);
                Console.WriteLine("ResponseUri: " + response.ResponseUri);
                string responseData = TokenFetcher.ReadResponseData(response.GetResponseStream());
                response.Close();
                return(responseData);
            }
            catch (WebException webExcp)
            {
                // If you reach this point, an exception has been caught.
                Console.WriteLine("A WebException has been caught.");
                // Write out the WebException message.
                Console.WriteLine(webExcp.ToString());
                // Get the WebException status code.
                WebExceptionStatus status = webExcp.Status;
                // If status is WebExceptionStatus.ProtocolError,
                //   there has been a protocol error and a WebResponse
                //   should exist. Display the protocol error.
                if (status == WebExceptionStatus.ProtocolError)
                {
                    Console.Write("The server returned protocol error ");
                    // Get HttpWebResponse so that you can check the HTTP status code.
                    HttpWebResponse httpResponse = (HttpWebResponse)webExcp.Response;

                    Console.WriteLine((int)httpResponse.StatusCode + " - "
                                      + httpResponse.StatusCode);
                }
                return(errorMessage);
            }
            catch (Exception e)
            {
                // Code to catch other exceptions goes here.
                Console.WriteLine(e.Message);
                return(errorMessage);
            }
        }