コード例 #1
0
        /// <summary>
        /// Gets a value indicating whether current connection is secured
        /// </summary>
        /// <returns>true - secured, false - not secured</returns>
        public virtual bool IsCurrentConnectionSecured()
        {
            bool useSsl = false;

            if (IsRequestAvailable(_httpContext))
            {
                //when your hosting uses a load balancer on their server then the Request.IsSecureConnection is never got set to true

                //1. use HTTP_CLUSTER_HTTPS?
                if (ConfigurationHelper.AppSettingsItem <bool>("HTTP_CLUSTER_HTTPS"))
                {
                    useSsl = ServerVariables("HTTP_CLUSTER_HTTPS") == "on";
                }
                //2. use HTTP_X_FORWARDED_PROTO?
                else if (ConfigurationHelper.AppSettingsItem <bool>("HTTP_X_FORWARDED_PROTO"))
                {
                    useSsl = string.Equals(ServerVariables("HTTP_X_FORWARDED_PROTO"), "https", StringComparison.OrdinalIgnoreCase);
                }
                else
                {
                    useSsl = _httpContext.Request.IsSecureConnection;
                }
            }

            return(useSsl);
        }
コード例 #2
0
        public static string GetTableName(this Type type)
        {
            var attr = type.GetCustomAttribute <TableAttribute>();

            if (attr == null)
            {
                var tableName = type.Name.ToPlural();
                if (ConfigurationHelper.AppSettingsItem <bool>("UseLowercaseTableNames"))
                {
                    tableName = tableName.ToLowerInvariant();
                }
                return(tableName);
            }
            return(attr.Name);
        }
コード例 #3
0
 public void SetLogger(object logger)
 {
     if (logger != null)
     {
         var loggr = logger as NLog.Logger;
         if (loggr != null)
         {
             _logger = loggr;
         }
     }
     else
     {
         _logger = LogManager.GetLogger(ConfigurationHelper.AppSettingsItem <string>("AppName") ?? "MultiTenancyFramework");
     }
 }
コード例 #4
0
        /// <summary>
        /// Get context IP address
        /// </summary>
        /// <returns>URL referrer</returns>
        public virtual string GetCurrentIpAddress()
        {
            if (!IsRequestAvailable(_httpContext))
            {
                return(string.Empty);
            }

            var result = "";

            try
            {
                if (_httpContext.Request.Headers != null)
                {
                    //The X-Forwarded-For (XFF) HTTP header field is a de facto standard
                    //for identifying the originating IP address of a client
                    //connecting to a web server through an HTTP proxy or load balancer.
                    var forwardedHttpHeader = "X-FORWARDED-FOR";
                    if (!string.IsNullOrWhiteSpace(ConfigurationHelper.AppSettingsItem <string>("ForwardedHTTPheader")))
                    {
                        //but in some cases server use other HTTP header
                        //in these cases an administrator can specify a custom Forwarded HTTP header
                        //e.g. CF-Connecting-IP, X-FORWARDED-PROTO, etc
                        forwardedHttpHeader = ConfigurationHelper.AppSettingsItem <string>("ForwardedHTTPheader");
                    }

                    //it's used for identifying the originating IP address of a client connecting to a web server
                    //through an HTTP proxy or load balancer.
                    string xff = _httpContext.Request.Headers.AllKeys
                                 .Where(x => forwardedHttpHeader.Equals(x, StringComparison.InvariantCultureIgnoreCase))
                                 .Select(k => _httpContext.Request.Headers[k])
                                 .FirstOrDefault();

                    //if you want to exclude private IP addresses, then see http://stackoverflow.com/questions/2577496/how-can-i-get-the-clients-ip-address-in-asp-net-mvc
                    if (!string.IsNullOrWhiteSpace(xff))
                    {
                        string lastIp = xff.Split(',')[0];
                        result = lastIp;
                    }
                }

                if (string.IsNullOrWhiteSpace(result))
                {
                    result = _httpContext.Request.UserHostAddress;
                }
            }
            catch { }

            if (string.IsNullOrWhiteSpace(result)) // should never happen
            {
                result = IPResolver.GetIP4Address();
            }

            //some validation
            if (result == "::1")
            {
                result = "127.0.0.1";
            }
            //remove port
            if (!string.IsNullOrWhiteSpace(result))
            {
                int index = result.IndexOf(":", StringComparison.InvariantCultureIgnoreCase);
                if (index > 0)
                {
                    result = result.Substring(0, index);
                }
            }
            return(result);
        }
コード例 #5
0
 public Logger()
 {
     _logger  = LogManager.GetLogger(ConfigurationHelper.AppSettingsItem <string>("AppName") ?? "MultiTenancyFramework");
     _logToDb = true;
 }