IPCheck: Allow only certain IP Address to access your DotNetNuke Module  (C#)

It may be desirable to allow access to a DotNetNuke module to only certain IP Addresses. The following module provides code that can be used to implement such a requirement.

The module compares the IP Address of the current visitor to a list of IP Addresses and permits or denies access.

 

Administrators can enter allowed IP Addresses. "*" can be used to enter entire IP Address blocks.

The Module

The module uses a single class, CheckIPAddress, to determine if an IP Address is valid or not:

        private string GetStatus(string IPAddress)
        {
            string Status = "You are not allowed to see this content.";
 
            if (IPCheck.IPAddress.CheckIPAddress(IPAddress))
            {
                Status = "You are allowed to see this content.";
            }
 
            return Status;
        }

The class is shown below:

        #region CheckIPAddress
        public static bool CheckIPAddress(string IPAddress)
        {
            // If the IP Address is not valid return false
            if (!(ValidateIPAddress(IPAddress)))
            {
                return false;
            }
 
            List<IPs> colIPs = DAL.GetIPAddressesCollection();
 
            // Loop through each part of the IP Address
            for (int i = 0; i <= 3; i++)
            {
                bool FoundPart = false;
 
                // Get the current part of the IP ddress
                string IPAddressPart = GetIPPart(IPAddress, i);
 
                // Loop through all the avaliable IP addresses
                foreach (IPs IP in colIPs)
                {
                    // If one of the available IP Address has a * for this block any IP will pass
                    // Otherwise the IP address must match at least one available IP Address
                    if (GetIPPart(IP.IP, i) == "*" || IPAddressPart == GetIPPart(IP.IP, i))
                    {
                        FoundPart = true;
                    }
                }
 
                if (!FoundPart)
                {
                    // The part was not found
                    return false;
                }
            }
 
            return true;
        } 
        #endregion

You can download the module here: IPCheck_01.00.00_Install.zip

[Back to: The ADefWebserver DotNetNuke HELP WebSite]


DotNetNuke® is a registered trademark of DotNetNuke Corporation