public void ClientCredentialsElement_defaults() { ClientCredentialsElement element = new ClientCredentialsElement(); Assert.AreEqual(typeof(ClientCredentials), element.BehaviorType, "BehaviorType"); Assert.AreEqual("clientCredentials", element.ConfigurationElementName, "ConfigurationElementName"); Assert.AreEqual(true, element.SupportInteractive, "SupportInteractive"); Assert.AreEqual(String.Empty, element.Type, "Type"); Assert.AreEqual(String.Empty, element.ClientCertificate.FindValue, "ClientCertificate.FindValue"); Assert.AreEqual(StoreLocation.CurrentUser, element.ClientCertificate.StoreLocation, "ClientCertificate.StoreLocation"); Assert.AreEqual(StoreName.My, element.ClientCertificate.StoreName, "ClientCertificate.StoreName"); Assert.AreEqual(X509FindType.FindBySubjectDistinguishedName, element.ClientCertificate.X509FindType, "ClientCertificate.X509FindType"); Assert.AreEqual(String.Empty, element.ServiceCertificate.DefaultCertificate.FindValue, "ServiceCertificate.DefaultCertificate.FindValue"); Assert.AreEqual(StoreLocation.CurrentUser, element.ServiceCertificate.DefaultCertificate.StoreLocation, "ServiceCertificate.DefaultCertificate.StoreLocation"); Assert.AreEqual(StoreName.My, element.ServiceCertificate.DefaultCertificate.StoreName, "ServiceCertificate.DefaultCertificate.StoreName"); Assert.AreEqual(X509FindType.FindBySubjectDistinguishedName, element.ServiceCertificate.DefaultCertificate.X509FindType, "ServiceCertificate.DefaultCertificate.X509FindType"); Assert.AreEqual(String.Empty, element.ServiceCertificate.Authentication.CustomCertificateValidatorType, "ServiceCertificate.Authentication.CustomCertificateValidatorType"); Assert.AreEqual(X509CertificateValidationMode.ChainTrust, element.ServiceCertificate.Authentication.CertificateValidationMode, "ServiceCertificate.Authentication.CertificateValidationMode"); Assert.AreEqual(X509RevocationMode.Online, element.ServiceCertificate.Authentication.RevocationMode, "ServiceCertificate.Authentication.RevocationMode"); Assert.AreEqual(StoreLocation.CurrentUser, element.ServiceCertificate.Authentication.TrustedStoreLocation, "ServiceCertificate.Authentication.TrustedStoreLocation"); Assert.AreEqual(true, element.Windows.AllowNtlm, "Windows.AllowNtlm"); Assert.AreEqual(TokenImpersonationLevel.Identification, element.Windows.AllowedImpersonationLevel, "Windows.AllowedImpersonationLevel"); Assert.AreEqual(true, element.IssuedToken.CacheIssuedTokens, "IssuedToken.CacheIssuedTokens"); Assert.AreEqual(SecurityKeyEntropyMode.CombinedEntropy, element.IssuedToken.DefaultKeyEntropyMode, "IssuedToken.DefaultKeyEntropyMode"); Assert.AreEqual(60, element.IssuedToken.IssuedTokenRenewalThresholdPercentage, "IssuedToken.IssuedTokenRenewalThresholdPercentage"); Assert.AreEqual(TokenImpersonationLevel.Identification, element.HttpDigest.ImpersonationLevel, "HttpDigest.ImpersonationLevel"); }
public virtual void GetDefaultCertificate() { var behaviors = (BehaviorsSection)ConfigurationManager.GetSection("system.serviceModel/behaviors"); EndpointBehaviorElement endpointBehavior = (EndpointBehaviorElement)behaviors.EndpointBehaviors["CustomBehavior"]; ClientCredentialsElement cce = (ClientCredentialsElement)endpointBehavior[0]; Thumbprint = cce.ClientCertificate.FindValue; SelectedCertificate = GetCertificateByThumbPrint(Thumbprint); HandleCertificateSet(); }
public void ClientBehaviorTest() { ServiceModelConfigurationManager manager = LoadManager(); ClientSection client = manager.GetClient(); NamedServiceModelExtensionCollectionElement <BehaviorExtensionElement> behavior = manager.GetBehavior(client.Endpoints[0].BehaviorConfiguration); ClientCredentialsElement credentialsSection = ServiceModelConfigurationManager.GetBehaviorExtensionElement <ClientCredentialsElement>(behavior); Assert.AreEqual(Constants.TestCert, credentialsSection.ClientCertificate.FindValue); Assert.AreEqual(System.ServiceModel.Security.X509CertificateValidationMode.ChainTrust, credentialsSection.ServiceCertificate.Authentication.CertificateValidationMode); }
private static void Main(string[] args) { config Config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ServiceModelSectionGroup Group = ServiceModelSectionGroup.GetSectionGroup(Config); BehaviorsSection Behaviors = Group.Behaviors; EndpointBehaviorElementCollection EndpointBehaviors = Behaviors.EndpointBehaviors; EndpointBehaviorElement EndpointBehavior = EndpointBehaviors[0]; ClientCredentialsElement ClientCredential = (ClientCredentialsElement)EndpointBehavior[0]; var ClientCertificate = ClientCredential.ClientCertificate; var findValue = ClientCertificate.FindValue; var storeName = ClientCertificate.StoreName; var storeLocation = ClientCertificate.StoreLocation; var X509FindType = ClientCertificate.X509FindType; }
/// <summary> /// Checks the specified configuration manager. /// </summary> /// <param name="configurationManager">The configuration manager.</param> /// <returns></returns> public override ProblemCollection Check(ServiceModelConfigurationManager configurationManager) { foreach (EndpointBehaviorElement behaviorElement in configurationManager.ServiceModelSection.Behaviors.EndpointBehaviors) { ClientCredentialsElement clientCredentials = ServiceModelConfigurationManager.GetBehaviorExtensionElement <ClientCredentialsElement>(behaviorElement); X509RevocationMode revocationMode = clientCredentials.ServiceCertificate.Authentication.RevocationMode; if (revocationMode == X509RevocationMode.NoCheck) { Resolution resolution = base.GetResolution(revocationMode.ToString()); Problem problem = new Problem(resolution); problem.SourceFile = base.SourceFile; base.Problems.Add(problem); } } return(base.Problems); }
public void ClientCredentialsElement() { EndpointBehaviorElement behavior = OpenConfig(); ClientCredentialsElement element = (ClientCredentialsElement)behavior [typeof(ClientCredentialsElement)]; if (element == null) { Assert.Fail("ClientCredentialsElement is not exist in collection."); } Assert.AreEqual(typeof(ClientCredentials), element.BehaviorType, "BehaviorType"); Assert.AreEqual("clientCredentials", element.ConfigurationElementName, "ConfigurationElementName"); Assert.AreEqual(false, element.SupportInteractive, "SupportInteractive"); Assert.AreEqual("ClientCredentialType", element.Type, "Type"); Assert.AreEqual("findValue", element.ClientCertificate.FindValue, "ClientCertificate.FindValue"); Assert.AreEqual(StoreLocation.LocalMachine, element.ClientCertificate.StoreLocation, "ClientCertificate.StoreLocation"); Assert.AreEqual(StoreName.Root, element.ClientCertificate.StoreName, "ClientCertificate.StoreName"); Assert.AreEqual(X509FindType.FindByExtension, element.ClientCertificate.X509FindType, "ClientCertificate.X509FindType"); Assert.AreEqual("findValue", element.ServiceCertificate.DefaultCertificate.FindValue, "ServiceCertificate.DefaultCertificate.FindValue"); Assert.AreEqual(StoreLocation.LocalMachine, element.ServiceCertificate.DefaultCertificate.StoreLocation, "ServiceCertificate.DefaultCertificate.StoreLocation"); Assert.AreEqual(StoreName.Root, element.ServiceCertificate.DefaultCertificate.StoreName, "ServiceCertificate.DefaultCertificate.StoreName"); Assert.AreEqual(X509FindType.FindByExtension, element.ServiceCertificate.DefaultCertificate.X509FindType, "ServiceCertificate.DefaultCertificate.X509FindType"); Assert.AreEqual("CustomCertificateValidatorType", element.ServiceCertificate.Authentication.CustomCertificateValidatorType, "ServiceCertificate.Authentication.CustomCertificateValidatorType"); Assert.AreEqual(X509CertificateValidationMode.None, element.ServiceCertificate.Authentication.CertificateValidationMode, "ServiceCertificate.Authentication.CertificateValidationMode"); Assert.AreEqual(X509RevocationMode.Offline, element.ServiceCertificate.Authentication.RevocationMode, "ServiceCertificate.Authentication.RevocationMode"); Assert.AreEqual(StoreLocation.LocalMachine, element.ServiceCertificate.Authentication.TrustedStoreLocation, "ServiceCertificate.Authentication.TrustedStoreLocation"); Assert.AreEqual(false, element.Windows.AllowNtlm, "Windows.AllowNtlm"); Assert.AreEqual(TokenImpersonationLevel.None, element.Windows.AllowedImpersonationLevel, "Windows.AllowedImpersonationLevel"); Assert.AreEqual(false, element.IssuedToken.CacheIssuedTokens, "IssuedToken.CacheIssuedTokens"); Assert.AreEqual(SecurityKeyEntropyMode.ClientEntropy, element.IssuedToken.DefaultKeyEntropyMode, "IssuedToken.DefaultKeyEntropyMode"); Assert.AreEqual(30, element.IssuedToken.IssuedTokenRenewalThresholdPercentage, "IssuedToken.IssuedTokenRenewalThresholdPercentage"); Assert.AreEqual(TokenImpersonationLevel.None, element.HttpDigest.ImpersonationLevel, "HttpDigest.ImpersonationLevel"); }
public int DoBinariesUpdate(out string sRemoteVersion, ref AutoResetEvent autoEvent, ref StartWindow winStarter) { // return values // 0 - no update needed // 1 - update done, do restart _autoEvent = autoEvent; _winStarter = winStarter; _sRemoteVersion = string.Empty; sRemoteVersion = string.Empty; string sRemoteURL = string.Empty; string[] arrVersions; _winStarter.SetMessage("Connecting to Version service"); Log.Info("Connecting to Version service"); X509Certificate2 Cert = new X509Certificate2(); bool bIsSSL = _sHUBAddress.Contains("https"); try { if (bIsSSL) { BehaviorsSection clientSection = ConfigurationManager.GetSection("system.serviceModel/behaviors") as BehaviorsSection; ServiceModelEnhancedConfigurationElementCollection <EndpointBehaviorElement> colX = clientSection.EndpointBehaviors; EndpointBehaviorElement ebe = colX[0]; ClientCredentialsElement clientCred = (ClientCredentialsElement)ebe.First(x => x.GetType() == typeof(ClientCredentialsElement)); string sFindName = clientCred.ClientCertificate.FindValue; string sStoreName = clientCred.ClientCertificate.StoreName.ToString(); X509Store store = new X509Store(sStoreName); store.Open(OpenFlags.ReadOnly); X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindBySubjectName, sFindName, false); Cert = certs[0]; } } catch (Exception ex) { Log.Error("Certificate file not found", ex); return(0); } try { HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(_sHUBAddress + "/BSMHub/release?version=1&app_type=terminal&client=" + _client); httpRequest.Method = "GET"; httpRequest.KeepAlive = false; ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); }; if (bIsSSL) { httpRequest.ClientCertificates.Add(Cert); } HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse(); Stream responseStream = httpResponse.GetResponseStream(); StreamReader sr = new StreamReader(responseStream); string sVersion = sr.ReadToEnd().ToString(); responseStream.Close(); httpResponse.Close(); httpRequest = null; //string sVersion = "111"; arrVersions = sVersion.Split((char)'\n'); _winStarter.SetMessage("Downloading updated version"); Log.Info("Got version information from " + _sHUBAddress + "/BSMHub/release?version=1&app_type=terminal&client=" + _client); } catch (Exception ex) { // log "no version file" Log.Error("Error getting remote version from " + _sHUBAddress + "/BSMHub/release?version=1&app_type=terminal&client=" + _client, ex); _winStarter.SetMessage("Error getting remote version"); return(0); } if (arrVersions[0].Length > 1) { _sRemoteVersion = arrVersions[0].Trim(); sRemoteVersion = _sRemoteVersion; } // compare versions string sLocalVersion = string.Empty; sRemoteURL = _sHUBAddress + "/BSMHub/release?id=SportBetting&app_type=terminal&client=" + _client; try { string sLocalVersionFile = Environment.CurrentDirectory + "/version.txt"; if (File.Exists(sLocalVersionFile)) { string[] sVersionInfoContent = File.ReadAllLines(sLocalVersionFile); if (sVersionInfoContent.Length > 0 && sVersionInfoContent[0] != null) { sLocalVersion = sVersionInfoContent[0].Trim(); } Log.Info("Local version: " + sLocalVersion); } else { sLocalVersion = "0"; Log.Info("Local version not found"); } } catch (Exception e) { Log.Error("Error opening version.txt file", e); } // compare versions. there might be upgrade and downgrade, so let's check for "not equal only" if (_sRemoteVersion != sLocalVersion) { try { _winStarter.SetMessage("Downloading new file version: " + _sRemoteVersion); // clean up update directory if (Directory.Exists(_sUpdatePath)) { Directory.Delete(_sUpdatePath, true); Thread.Sleep(500); } Directory.CreateDirectory(_sUpdatePath); Thread.Sleep(500); string sDownloadLocalFile = _sUpdatePath + "\\download" + _sRemoteVersion + ".zip"; HttpWebRequest httpRequestZip = (HttpWebRequest)WebRequest.Create(sRemoteURL); if (bIsSSL) { httpRequestZip.ClientCertificates.Add(Cert); } HttpWebResponse httpResponseZip = (HttpWebResponse)httpRequestZip.GetResponse(); int uFileSize; int.TryParse(httpResponseZip.Headers.Get("Content-Length"), out uFileSize); Log.Info("download file size: " + uFileSize); byte[] buffer = new byte[32768]; using (Stream input = httpResponseZip.GetResponseStream()) { using (FileStream output = new FileStream(sDownloadLocalFile, FileMode.CreateNew)) { long dlBytes = 0; int bytesRead = 0; int dlStep = 1; int dlPercentNext = 0; double dlPercent = 0; while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0) { output.Write(buffer, 0, bytesRead); dlBytes += bytesRead; dlPercent = dlBytes * 100 / uFileSize; if (dlPercentNext == dlPercent) { dlPercentNext += dlStep; _winStarter.SetMessageControlledSleep("Download progress: " + dlPercent + "% (" + dlBytes + "/" + uFileSize + ")", 25); } } } } httpResponseZip.Close(); Log.Info("Got remote ZIP file with new version"); ProcessDownloadedFile(); ThreadPool.QueueUserWorkItem(new WaitCallback(ReleaseThread), _autoEvent); } catch (Exception ex) { // log "no version file" Log.Error("Error getting remote version from " + _sHUBAddress + "/BSMHub/release?versions", ex); _winStarter.SetMessage("Error getting remote version"); return(0); } } else { // no update needed _winStarter.SetMessage("No update needed"); return(0); } return(1); }