public void OnAuthorization(AuthorizationFilterContext context)
        {
            _Logger.LogInformation("Starting admin ip verification.");
            var  connection = context.HttpContext.Connection;
            bool isLocal    = IslolcalRequest(context);

            if (isLocal == false)
            {
                _Logger.LogInformation("Remote IP detected.Comparing the IP with admin IPs in the settings.");
                bool isAllowed = _Settings.GetAdminIps().Contains(connection.RemoteIpAddress.ToString());
                if (isAllowed == false)
                {
                    _Logger.LogWarning($"Remote IP '{connection.RemoteIpAddress.ToString()}' is not found in the privileged IP list.Access Denied.");
                    context.Result = new UnauthorizedResult()
                    {
                    };
                }
                else
                {
                    _Logger.LogInformation($"Remote IP '{connection.RemoteIpAddress.ToString()}' is found in the privileged IP list.");
                }
            }
            else
            {
                _Logger.LogInformation("Local request detected.Skipping IP checking.");
            }
        }
示例#2
0
 /// <summary>
 /// Determines whether given ip address is an admin IP
 /// </summary>
 /// <param name="ipAddress">The ip address you want to check.</param>
 /// <param name="settings">The settings reader.</param>
 /// <returns>
 ///   <c>true</c> if given IP is an admin IP; otherwise, <c>false</c>.
 /// </returns>
 public static bool IsInPrivilegedIpList(string ipAddress, IDbSettingsReader settings)
 {
     if (!string.IsNullOrWhiteSpace(ipAddress))
     {
         var addresses = settings.GetAdminIps();
         return(addresses.Where(a => a.Trim().Equals(ipAddress, StringComparison.InvariantCultureIgnoreCase)).Any());
     }
     return(false);
 }