Example #1
0
        public string CallDemoServiceWithAuthorityContext(string message, string endpointURL)
        {
            // Security protocols supported by the DemoService.
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            DemoPortTypeClient demoPortType = new DemoPortTypeClient();

            if (endpointURL != null)
            {
                EndpointAddress endpointAddress = new EndpointAddress(new Uri(endpointURL));
                demoPortType.Endpoint.Address = endpointAddress;
            }

            DemoPortType channel = demoPortType.ChannelFactory.CreateChannel();

            callDemoServiceRequest request = new callDemoServiceRequest()
            {
                CallDemoServiceRequest1 = new CallDemoServiceRequestType()
                {
                    messageString    = message,
                    AuthorityContext = GetAuthorityContext(),
                    CallContext      = GetCallContext()
                }
            };

            // 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;

            var response = channel.callDemoService(request);

            return(response.CallDemoServiceResponse1.responseString);
        }
        private DemoPortType CreateChannel(SecurityToken token, string endpointUrl)
        {
            DemoPortTypeClient demoPortType = new DemoPortTypeClient();

            // Disable revocation checking (do not use in production).
            // Should be uncommented if you intent to call DemoService locally.
            // demoPortType.ClientCredentials.ServiceCertificate.Authentication.RevocationMode = System.Security.Cryptography.X509Certificates.X509RevocationMode.NoCheck;
            EndpointIdentity identity        = EndpointIdentity.CreateDnsIdentity(ConfigVariables.ServiceCertificateAlias);
            EndpointAddress  endpointAddress = endpointUrl != null ? new EndpointAddress(new Uri(endpointUrl), identity) : new EndpointAddress(demoPortType.Endpoint.Address.Uri, identity);

            demoPortType.Endpoint.Address = endpointAddress;
            var certificate = CertificateLoader.LoadCertificate(
                ConfigVariables.ClientCertificateStoreName,
                ConfigVariables.ClientCertificateStoreLocation,
                ConfigVariables.ClientCertificateThumbprint);

            demoPortType.ClientCredentials.ClientCertificate.Certificate = certificate;
            demoPortType.Endpoint.Contract.ProtectionLevel = ProtectionLevel.Sign;

            return(demoPortType.ChannelFactory.CreateChannelWithIssuedToken(token));
        }