public static bool IsAllowed(string ipAddress, List <string> blockList, List <string> allowList) { var isAllowed = true; if (blockList != null && blockList.Count > 0) { var list = new IpList(); foreach (var restriction in blockList) { AddRestrictionToIpList(list, restriction); } if (list.CheckNumber(ipAddress)) { isAllowed = false; } } else if (allowList != null && allowList.Count > 0) { isAllowed = false; var list = new IpList(); foreach (var restriction in allowList) { AddRestrictionToIpList(list, restriction); } if (list.CheckNumber(ipAddress)) { isAllowed = true; } } return(isAllowed); }
/// <summary> /// Checks the denied IPs. /// </summary> /// <param name="userIpAddress">The user ip address.</param> /// <returns></returns> private bool CheckDeniedIPs(string userIpAddress) { // Populate the IPList with the Single IPs if (!string.IsNullOrEmpty(DeniedSingleIPs)) { SplitAndAddSingleIPs(DeniedSingleIPs, _deniedIpListToCheck); } // Populate the IPList with the Masked IPs if (!string.IsNullOrEmpty(DeniedMaskedIPs)) { SplitAndAddMaskedIPs(DeniedMaskedIPs, _deniedIpListToCheck); } // Check if there are more settings from the configuration (Web.config) if (!string.IsNullOrEmpty(ConfigurationKeyDeniedSingleIPs)) { string configurationDeniedAdminSingleIPs = ConfigurationManager.AppSettings[ConfigurationKeyDeniedSingleIPs]; if (!string.IsNullOrEmpty(configurationDeniedAdminSingleIPs)) { SplitAndAddSingleIPs(configurationDeniedAdminSingleIPs, _deniedIpListToCheck); } } if (!string.IsNullOrEmpty(ConfigurationKeyDeniedMaskedIPs)) { string configurationDeniedAdminMaskedIPs = ConfigurationManager.AppSettings[ConfigurationKeyDeniedMaskedIPs]; if (!string.IsNullOrEmpty(configurationDeniedAdminMaskedIPs)) { SplitAndAddMaskedIPs(configurationDeniedAdminMaskedIPs, _deniedIpListToCheck); } } return(_deniedIpListToCheck.CheckNumber(userIpAddress)); }
public static bool IsVisitAllowed(ISettingsManager settingsManager, HttpRequest request) { var isAllowed = true; if (!string.IsNullOrEmpty(settingsManager.AdminRestrictionHost)) { var currentHost = RemoveProtocolFromUrl(GetHost(request)); if (!StringUtils.StartsWithIgnoreCase(currentHost, RemoveProtocolFromUrl(settingsManager.AdminRestrictionHost))) { isAllowed = false; } } if (isAllowed) { var userIp = GetIpAddress(request); if (settingsManager.AdminRestrictionBlockList != null && settingsManager.AdminRestrictionBlockList.Length > 0) { var list = new IpList(); foreach (var restriction in settingsManager.AdminRestrictionBlockList) { AddRestrictionToIpList(list, restriction); } if (list.CheckNumber(userIp)) { isAllowed = false; } } else if (settingsManager.AdminRestrictionAllowList != null && settingsManager.AdminRestrictionAllowList.Length > 0) { isAllowed = false; var list = new IpList(); foreach (var restriction in settingsManager.AdminRestrictionAllowList) { AddRestrictionToIpList(list, restriction); } if (list.CheckNumber(userIp)) { isAllowed = true; } } } return(isAllowed); }
public static bool IsVisitAllowed(Config config) { var restrictionType = ERestrictionTypeUtils.GetEnumType(config.IpRestrictionType); var restrictionList = new List <string>(); if (restrictionType == ERestrictionType.BlackList) { restrictionList = new List <string>(config.IpBlackList.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)); } else if (restrictionType == ERestrictionType.WhiteList) { restrictionList = new List <string>(config.IpWhiteList.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)); } var isAllowed = true; if (restrictionType != ERestrictionType.None) { var userIp = GetIpAddress(); if (restrictionType == ERestrictionType.BlackList) { var list = new IpList(); foreach (var restriction in restrictionList) { AddRestrictionToIpList(list, restriction); } if (list.CheckNumber(userIp)) { isAllowed = false; } } else if (restrictionType == ERestrictionType.WhiteList) { if (restrictionList.Count > 0) { isAllowed = false; var list = new IpList(); foreach (var restriction in restrictionList) { AddRestrictionToIpList(list, restriction); } if (list.CheckNumber(userIp)) { isAllowed = true; } } } } if (isAllowed) { if (config.IsHostRestriction && !string.IsNullOrEmpty(config.Host)) { var currentHost = RemoveProtocolFromUrl(GetHost()); if (!StartsWithIgnoreCase(currentHost, RemoveProtocolFromUrl(config.Host))) { isAllowed = false; } } } return(isAllowed); }