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

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

, 15 Jun 2009 CPOL
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.Configuration;
using System.Xml;

namespace IPFiltering.Configuration
{

    public class FilterConfiguration : ConfigurationElement
    {
        private const string _allowElementName = "allow";
        private const string _denyElementName = "deny";
        private const string _hostAttributeName = "hosts";
        private const string _filterName = "Name";
        private const string _defaultBehaviorAttribName = "DefaultBehavior";
        private IList<FilterConfigurationItem> _filterItems = null;

        /// <summary>
        /// Gets the name of the filter.
        /// </summary>
        /// <value>The name.</value>
        [ConfigurationProperty(_filterName, IsRequired = true)]
        public string Name
        {
            get
            {
                return this[_filterName] as string;
            }
        }

        /// <summary>
        /// Gets the default behavior when no match is found.
        /// </summary>
        /// <value>The default behavior.</value>
        [ConfigurationProperty(_defaultBehaviorAttribName, IsRequired = false, DefaultValue = IPFilterType.Allow)]
        public IPFilterType DefaultBehavior
        {
            get
            {
                return (IPFilterType)this[_defaultBehaviorAttribName];
            }
        }

        /// <summary>
        /// Gets the filters.
        /// </summary>
        /// <value>The filters.</value>
        public IList<FilterConfigurationItem> Filters
        {
            get
            {
                return _filterItems;
            }
        }
        
        /// <summary>
        /// Gets a value indicating whether an unknown element is encountered during deserialization.
        /// </summary>
        /// <param name="elementName">The name of the unknown subelement.</param>
        /// <param name="reader">The <see cref="T:System.Xml.XmlReader"/> being used for deserialization.</param>
        /// <returns>
        /// true when an unknown element is encountered while deserializing; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.Configuration.ConfigurationErrorsException">The element identified by <paramref name="elementName"/> is locked.- or -One or more of the element's attributes is locked.- or -<paramref name="elementName"/> is unrecognized, or the element has an unrecognized attribute.- or -The element has a Boolean attribute with an invalid value.- or -An attempt was made to deserialize a property more than once.- or -An attempt was made to deserialize a property that is not a valid member of the element.- or -The element cannot contain a CDATA or text element.</exception>
        protected override bool OnDeserializeUnrecognizedElement(string elementName, XmlReader reader)
        {

            IPFilterType filterType = GetFilterResult(elementName);
            if (filterType == IPFilterType.NoMatch)
            {
                return base.OnDeserializeUnrecognizedElement(elementName, reader);
            }
            string host = null;
            while (reader.MoveToNextAttribute())
            {
                if (reader.Name == _hostAttributeName)
                {
                    host = reader.Value;
                }
                else
                {
                    throw new ConfigurationErrorsException("Unknown attribute " + reader.Name);
                }
            }
            if (host == null)
            {
                throw new ConfigurationErrorsException("Host attribute not found.");
            }
            _filterItems.Add(new FilterConfigurationItem() { FilterType = filterType, Hosts = host });

            return true;
            
        }
        /// <summary>
        /// Sets the <see cref="T:System.Configuration.ConfigurationElement"/> object to its initial state.
        /// </summary>
        protected override void Init()
        {
            base.Init();
            _filterItems = new List<FilterConfigurationItem>();
        }

        private static IPFilterType GetFilterResult(string  value)
        {
            if (value == _allowElementName)
            {
                return IPFilterType.Allow;
            }
            else if (value == _denyElementName)
            {
                return IPFilterType.Deny;
            }
            return IPFilterType.NoMatch;
        }


    }
}

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 | Terms of Use | Mobile
Web01 | 2.8.1411019.1 | Last Updated 16 Jun 2009
Article Copyright 2009 by Ziad Elmalki
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid