Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

WCF Service Behavior Example: IPFilter - Allow/Deny Access by IP Address

, 15 Jun 2009
WCF Service Behavior Example: IPFilter - Allow/Deny Access by IP Address
IPFilterExample.zip
IPFilterTests
bin
Debug
IPFilter.dll
IPFilterTests.dll
Configuration
Mock
Properties
TestWebsite
App_Data
bin
IPFilter.dll
IPFilter.pdb
TestWebsite.dll
TestWebsite.pdb
Filtered
Properties
TestWebsite.csproj.user
IPFilterExample.suo
IPFilterExample.vsmdi
IPFilterExample1.vsmdi
IPFilterExample2.vsmdi
LocalTestRun.testrunconfig
IPFilter
bin
Debug
IPFilter.dll
Configuration
Properties
IPFilterExamples.zip
IPFilterExample3.vsmdi
LocalTestRun.testrunconfig
IPFilter.dll
IPFilter.csproj.user
IPFilter.dll
IPFilterTests.dll
IPFilter.dll
IPFilter.pdb
TestWebsite.dll
TestWebsite.pdb
TestWebsite.csproj.user
IPFilterExample.vsmdi
IPFilterExample1.vsmdi
IPFilterExample2.vsmdi
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace IPFiltering
{
    public class IPFilter
    {
        private string _name;
        private IList<IPFilterItem> _items;
        private IPFilterType _defaultBehavior;

        public IPFilter(string name,IList<IPFilterItem> items, IPFilterType defaultBehavior)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("The parameter cannot be null or an empty string.", "name");
            }
            if (items == null)
            {
                throw new ArgumentNullException("items");
            }
            _name = name;
            _items = items;
            _defaultBehavior = defaultBehavior;
        }

        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name
        {
            get
            {
                return _name;
            }
        }
        /// <summary>
        /// Gets the filter items
        /// </summary>
        /// <value>The items.</value>
        public IList<IPFilterItem> Items
        {
            get
            {
                return _items;
            }
        }
        /// <summary>
        /// Gets the behavior when no matches are found.
        /// </summary>
        /// <value>The default behavior.</value>
        public IPFilterType DefaultBehavior
        {
            get
            {
                return _defaultBehavior;
            }
        }

        /// <summary>
        /// Checks the address.
        /// </summary>
        /// <param name="ipAddress">The ip address.</param>
        /// <returns></returns>
        public IPFilterType CheckAddress(IPAddress ipAddress)
        {
            if (ipAddress.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork)
            {
                IPAddress address = GetIPv4Address(ipAddress);
                if (address == null)
                {
                    throw new NotSupportedException("IPv4 supported only.  Address Family : " + ipAddress.AddressFamily.ToString());
                }
                ipAddress = address;
            }
            return CheckAddress((uint)ipAddress.Address);
        }
        /// <summary>
        /// Checks the address.
        /// </summary>
        /// <param name="ipAddress">The ip address.</param>
        /// <returns></returns>
        public IPFilterType CheckAddress(uint ipAddress)
        {
            IPFilterType result;
            for (int i = 0; i < _items.Count; i++)
            {
                result = _items[i].CheckAddress(ipAddress);
                if (result != IPFilterType.NoMatch)
                {
                    return result;
                }
            }
            return _defaultBehavior;
        }

        /// <summary>
        /// Gets an ip filter that allows all incoming requests.
        /// </summary>
        /// <value></value>
        public static IPFilter Allow
        {
            get
            {
                return new IPFilter("Allow", new IPFilterItem[0], IPFilterType.Allow);
            }
        }

        /// <summary>
        /// Gets an ip filter that denies all incoming requests.
        /// </summary>
        /// <value></value>
        public static IPFilter Deny
        {
            get
            {
                return new IPFilter("Deny", new IPFilterItem[0], IPFilterType.Deny);
            }
        }

        private static IPAddress GetIPv4Address(IPAddress address)
        {
            if (IPAddress.IPv6Loopback.Equals(address))
            {
                return new IPAddress(0x0100007F);
            }
            IPAddress[] addresses = Dns.GetHostAddresses(address.ToString());
            return addresses.FirstOrDefault(i => i.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
        }
    }


}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ziad Elmalki

United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 16 Jun 2009
Article Copyright 2009 by Ziad Elmalki
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid