Esempio n. 1
0
        public LogHelper()
        {
            var _configPrefix = typeof(LogHelper).Name + "_";

            _SP = new Configuration.SettingsParser();
            _SP.ConfigPrefix = _configPrefix;
            _FileWriter      = new IO.LogFileWriter(_configPrefix);
        }
Esempio n. 2
0
        public LogFileWriter(string configPrefix = null)
        {
            // This class can be inherited therefore use type to make sure that prefix is different.
            // Get type will return derived class or this class if not derived.
            var prefix = configPrefix ?? GetType().Name;

            // Add separator to the prefix if missing.
            if (!prefix.EndsWith("_", StringComparison.OrdinalIgnoreCase) && !prefix.EndsWith("-", StringComparison.OrdinalIgnoreCase))
            {
                prefix += "_";
            }
            _SP = new Configuration.SettingsParser();
            _SP.ConfigPrefix = prefix;
            _Init();
        }
Esempio n. 3
0
        /* Example:
         *
         * /// <summary>Class with sensitive property.</summary>
         * public class ClassName
         * {
         *      /// <summary>Sensitive Property.</summary>
         *      public string Password
         *      {
         *              get { return SecurityHelper.GetSecureStringValue(_Password); }
         *              set { SecurityHelper.SetSecureStringValue(ref _Password, value); }
         *      }
         *      SecureString _Password;
         *
         *      ~ClassName()
         *      {
         *              SecurityHelper.SetSecureStringValue(ref _Password, null);
         *      }
         * }
         *
         */

        #endregion

        /// <summary>
        /// The following method is invoked by the RemoteCertificateValidationDelegate.
        /// Net.ServicePointManager.ServerCertificateValidationCallback = AddressOf ValidateServerCertificate
        /// </summary>
        /// <remarks>
        /// Add "AllowCertificateErrors" to allow certificate errors: request.Headers.Add("AllowCertificateErrors");
        /// </remarks>
        public static bool ValidateServerCertificate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
        {
            // No errors were found.
            if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
            {
                // Allow this client to communicate with unauthenticated servers.
                return(true);
            }
            var    sp      = new Configuration.SettingsParser("CertificateErrors_");
            var    allow   = sp.Parse("Allow", false);
            var    notify  = sp.Parse("Notify", true);
            string message = string.Format("Certificate error: {0}", sslPolicyErrors);

            message += allow
                                ? " Allow this client to communicate with unauthenticated server."
                                : " The underlying connection was closed.";
            var ex = new Exception("Validate server certificate error");

            ex.Data.Add("AllowCertificateErrors", allow);
            if (sender != null && sender is System.Net.HttpWebRequest)
            {
                //var request = (System.Net.HttpWebRequest)sender;
                // Allow certificate errors if request contains "AllowCertificateErrors" key.
                //AllowCertificateErrors = request.Headers.AllKeys.Contains("AllowCertificateErrors");
                var hr = (System.Net.HttpWebRequest)sender;
                ex.Data.Add("sender.OriginalString", hr.Address.OriginalString);
            }
            if (certificate != null)
            {
                ex.Data.Add("Certificate.Issuer", certificate.Issuer);
                ex.Data.Add("Certificate.Subject", certificate.Subject);
            }
            if (chain != null)
            {
                for (int i = 0; i < chain.ChainStatus.Length; i++)
                {
                    ex.Data.Add("Chain.ChainStatus(" + i + ")", string.Format("{0}, {1}", chain.ChainStatus[i].Status, chain.ChainStatus[i].StatusInformation));
                }
            }
            if (notify)
            {
                Runtime.LogHelper.Current.ProcessException(ex);
            }
            // Allow (or not allow depending on setting value) this client to communicate with unauthenticated servers.
            return(allow);
        }