static void Main(string[] args) { Trace.TraceInformation("Custom, started"); ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(OnValidationCallback); ServicePointManager.Expect100Continue = true; ServicePointManager.FindServicePoint(new Uri(ConfigurationManager.AppSettings["SyncExtSSL"])).MaxIdleTime = 3600000; //ServicePointManager.SetTcpKeepAlive(true, 60*1000, 30); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;//(SecurityProtocolType)3072; //SecurityProtocolType.Tls12; var message = new eHtalkMessage { EnvelopeVersion = "2.0", Header = new eHtalkMessageHeader { MessageInfo = new eHtalkMessageHeaderMessageInfo { Class = "Ping", MessageID = Guid.NewGuid().ToString("D"), CorrelationID = Guid.NewGuid().ToString("D"), BusinessID = "", ChannelInfoReply = "" }, SenderInfo = new eHtalkMessageHeaderSenderInfo { SecurityToken = new eHtalkMessageHeaderSenderInfoSecurityToken { }, UserContext = new UserContext { IdentifikatorOUPZS = new UserContextIdentifikatorOUPZS { rootOID = "1.3.158.00165387.100.40.70", extension = "00000000000" }, Specialization = new UserContextSpecialization { codeValue = "00000000000", codingSchemeOID = "1.3.158.00165387.100.10.34", codingSchemeVersion = "1" } } }, }, }; message.Body = new eHtalkMessageBody { Data = new eHtalkMessageBodyData { }, Result = new eHtalkMessageBodyResult { Code = "0" } }; var stopw = new Stopwatch(); stopw.Start(); X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser); store.Open(OpenFlags.ReadOnly); X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates; X509Certificate2Collection fcollection = (X509Certificate2Collection)collection.Find(X509FindType.FindByTimeValid, DateTime.Now, false); X509Certificate2Collection zcollection = (X509Certificate2Collection)fcollection.Find(X509FindType.FindByIssuerName, "NCZI PreProd HPRO Authentication CA R1-1", false); X509Certificate2Collection scollection = X509Certificate2UI.SelectFromCollection(zcollection, "Test Certificate Select", "Select a certificate from the following list to get information on that certificate", X509SelectionFlag.SingleSelection); Console.WriteLine("Number of certificates: {0}{1}", scollection.Count, Environment.NewLine); var result = CallHelper.GetResponseSync(message, new X509Certificate2(Assembly.GetExecutingAssembly().GetManifestResourceStream("SSLKlient.services_preprod_npz_sk.cer").ReadAllBytes()), ConfigurationManager.AppSettings["SyncExtSSL"], ConfigurationManager.AppSettings["TargetServiceIdentifier"], String.Format("{0}{1}", ConfigurationManager.AppSettings["IamStsServiceBaseUri"], "Trust/2005/CertificateTransport/"), scollection[0], ConfigurationManager.AppSettings["SyncExt"], stopw); var ser = new XmlSerializer(typeof(eHtalkMessage)); var sw = new StringWriter(); ser.Serialize(sw, result); Console.WriteLine(result.Body.Result.Code); Console.WriteLine(string.Format("Mam odpoved od ESB: {0}", stopw.ElapsedMilliseconds)); Console.ReadLine(); }
public static eHtalkMessage GetResponseSync(eHtalkMessage msg, X509Certificate2 extInterfaCertificate, string esbEndpoint, string relyingParty, string identityProviderURL, X509Certificate2 userCertificate, string wsaddressingTo, Stopwatch stopw) { #if !CC IssuedSecurityTokenProvider provider = new IssuedSecurityTokenProvider(); provider.SecurityTokenSerializer = new WSSecurityTokenSerializer(); provider.TargetAddress = new EndpointAddress(new Uri(relyingParty), new AddressHeader[0]); provider.IssuerAddress = new EndpointAddress(new Uri(identityProviderURL), new AddressHeader[0]); provider.SecurityAlgorithmSuite = SecurityAlgorithmSuite.Basic256; provider.MessageSecurityVersion = MessageSecurityVersion.WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10; ClientCredentials credentials = new ClientCredentials { ClientCertificate = { Certificate = userCertificate } }; provider.IssuerChannelBehaviors.Add(credentials); HttpsTransportBindingElement tbe = new HttpsTransportBindingElement { AuthenticationScheme = AuthenticationSchemes.Digest, RequireClientCertificate = true, KeepAliveEnabled = false }; CustomBinding stsBinding = new CustomBinding(new BindingElement[] { tbe }); provider.IssuerBinding = stsBinding; provider.Open(); var token = provider.GetToken(TimeSpan.FromSeconds(30.0)) as GenericXmlSecurityToken; #endif #if CC var cc = new EhealthCryptoController(); var token = cc.GetSamlTokenForHealthProfessional(relyingParty); #endif if (token == null) { throw new ApplicationException("No AT token received"); } Console.WriteLine(string.Format("Ziskany AT token in {0}", stopw.ElapsedMilliseconds)); CustomBinding binding = new CustomBinding(); SecurityBindingElement sbe = SecurityBindingElement.CreateIssuedTokenForCertificateBindingElement(new IssuedSecurityTokenParameters() { RequireDerivedKeys = true, KeyType = SecurityKeyType.SymmetricKey }); sbe.MessageSecurityVersion = MessageSecurityVersion.WSSecurity11WSTrust13WSSecureConversation13WSSecurityPolicy12BasicSecurityProfile10; sbe.SecurityHeaderLayout = SecurityHeaderLayout.Strict; sbe.IncludeTimestamp = true; //sbe.AllowInsecureTransport = true; sbe.SetKeyDerivation(true); sbe.KeyEntropyMode = SecurityKeyEntropyMode.ClientEntropy; binding.Elements.Add(sbe); binding.Elements.Add(new TextMessageEncodingBindingElement(MessageVersion.Soap12WSAddressing10, System.Text.Encoding.UTF8)); binding.Elements.Add(new HttpsTransportBindingElement() { RequireClientCertificate = true, KeepAliveEnabled = true }); var regEx = new Regex(@"https?://([^/]+)"); var dnsIdentity = regEx.Match(wsaddressingTo).Groups[1].Captures[0].Value; var channelFactory = new ChannelFactory <IeHealthSyncService>(binding, new EndpointAddress( new Uri(wsaddressingTo), new DnsEndpointIdentity(dnsIdentity), new AddressHeader[] { })); channelFactory.Credentials.SupportInteractive = false; channelFactory.Credentials.ClientCertificate.Certificate = userCertificate; channelFactory.Credentials.ServiceCertificate.DefaultCertificate = extInterfaCertificate; channelFactory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None; channelFactory.ConfigureChannelFactory <IeHealthSyncService>(); channelFactory.Endpoint.Behaviors.Add(new ClientViaBehavior(new Uri(esbEndpoint))); var channel = channelFactory.CreateChannelWithIssuedToken(token); Console.WriteLine(string.Format("vytvoreny kanal: {0}", stopw.ElapsedMilliseconds)); var stopw1 = new Stopwatch(); eHtalkMessage data = null; int wait = 1; for (int i = 0; i < 20; i++) { stopw1.Reset(); stopw1.Start(); msg.Header.MessageInfo.MessageID = Guid.NewGuid().ToString("D"); Debug.WriteLine("Start calling", "MyCustom"); try { data = channel.GetData(msg); } catch (CommunicationException ex) { data = channel.GetData(msg); } Console.WriteLine(string.Format("po {1} sekundach: {0}", stopw1.ElapsedMilliseconds, wait)); Thread.Sleep(wait * 1000); wait = wait * 2; } return(data); }