Esempio n. 1
0
        /// <summary>
        /// Creates an XML reader from the provided URL with the appropriate configured connection.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="defaultProxy">The default System proxy which is passed in due to issues retrieving this on a separate thread.</param>
        /// <returns></returns>
        public static XmlReader CreateXmlReader(string url, IWebProxy defaultProxy)
        {
            //Default to use No proxy settings with Windows Authentication
            IWebProxy prox = null;
            var cred = CredentialCache.DefaultCredentials;
            string certPath = null;

            //Check settings Manager for values
            SettingsManager.LoadSettings();
            if (SettingsManager.Settings.Connectivity != null)
            {
                //Proxy
                if (SettingsManager.Settings.Connectivity.Proxy.Item != null)
                {
                    switch (SettingsManager.Settings.Connectivity.Proxy.ItemElementName)
                    {
                        case ProxyType.WindowsProxy:
                            prox = defaultProxy;
                            break;
                        case ProxyType.ManualProxy:
                            var manproxy = (ManualProxy) SettingsManager.Settings.Connectivity.Proxy.Item;
                            prox = new WebProxy
                                       {
                                           Address = new Uri(manproxy.Address + ":" + manproxy.Port),
                                           BypassProxyOnLocal = manproxy.BypassLocal
                                       };
                            break;
                    }
                }

                //Auth
                if (SettingsManager.Settings.Connectivity.Authentication.Item != null && !(SettingsManager.Settings.Connectivity.Authentication.Item is bool))
                {
                    var credentials = (ProvidedCredentials) SettingsManager.Settings.Connectivity.Authentication.Item;
                    cred = new NetworkCredential(credentials.Username, credentials.Password);
                }

                if (prox != null)
                {
                    prox.Credentials = cred;
                }

                //SSL
                certPath = SettingsManager.Settings.Connectivity.SSLCertPath;
            }

            //Also pass credentials to web client so it can authenticate against servers that do not allow anonymous connections
            var client = new CertWebClient { Proxy = prox, Credentials = cred, CertPath = certPath };

            //Provide support for SSL by accepting all certificates
            ServicePointManager.ServerCertificateValidationCallback += delegate { return true; };

            //Read the url
            if (url != null)
            {
                var rmstream = client.OpenRead(url);
                if (rmstream != null)
                {
                    var reader = XmlReader.Create(rmstream, new XmlReaderSettings { ProhibitDtd = false, CheckCharacters = false });

                    return reader;
                }
            }
            return null;
        }
Esempio n. 2
0
        /// <summary>
        /// Creates an XML reader from the provided URL with the appropriate configured connection.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="defaultProxy">The default System proxy which is passed in due to issues retrieving this on a separate thread.</param>
        /// <returns></returns>
        public static XmlReader CreateXmlReader(string url, IWebProxy defaultProxy)
        {
            //Default to use No proxy settings with Windows Authentication
            IWebProxy prox     = null;
            var       cred     = CredentialCache.DefaultCredentials;
            string    certPath = null;

            //Check settings Manager for values
            SettingsManager.LoadSettings();
            if (SettingsManager.Settings.Connectivity != null)
            {
                //Proxy
                if (SettingsManager.Settings.Connectivity.Proxy.Item != null)
                {
                    switch (SettingsManager.Settings.Connectivity.Proxy.ItemElementName)
                    {
                    case ProxyType.WindowsProxy:
                        prox = defaultProxy;
                        break;

                    case ProxyType.ManualProxy:
                        var manproxy = (ManualProxy)SettingsManager.Settings.Connectivity.Proxy.Item;
                        prox = new WebProxy
                        {
                            Address            = new Uri(manproxy.Address + ":" + manproxy.Port),
                            BypassProxyOnLocal = manproxy.BypassLocal
                        };
                        break;
                    }
                }

                //Auth
                if (SettingsManager.Settings.Connectivity.Authentication.Item != null && !(SettingsManager.Settings.Connectivity.Authentication.Item is bool))
                {
                    var credentials = (ProvidedCredentials)SettingsManager.Settings.Connectivity.Authentication.Item;
                    cred = new NetworkCredential(credentials.Username, credentials.Password);
                }

                if (prox != null)
                {
                    prox.Credentials = cred;
                }

                //SSL
                certPath = SettingsManager.Settings.Connectivity.SSLCertPath;
            }

            //Also pass credentials to web client so it can authenticate against servers that do not allow anonymous connections
            var client = new CertWebClient {
                Proxy = prox, Credentials = cred, CertPath = certPath
            };

            //Provide support for SSL by accepting all certificates
            ServicePointManager.ServerCertificateValidationCallback += delegate { return(true); };

            //Support TLS 1.2 by manually providing a proper value for the certificate. Other versions of the protocol are listed below.
            //Ssl3 = 48, Tls = 192, Tls11 = 768, Tls12 = 3072
            //ServicePointManager.SecurityProtocol = (SecurityProtocolType)48 | (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
            //Author: Kenan Dervišević
            //Company: Siemens Mobility
            ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

            //Read the url
            if (url != null)
            {
                var rmstream = client.OpenRead(url);
                if (rmstream != null)
                {
                    var reader = XmlReader.Create(rmstream, new XmlReaderSettings {
                        ProhibitDtd = false, CheckCharacters = false
                    });

                    return(reader);
                }
            }
            return(null);
        }