DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Dennis has posted 5 posts at DZone. View Full User Profile

Authorize Client IP With Custom Config Settings

12.01.2009
| 1386 views |
  • submit to reddit
        Authorize Client IP with Custom Config Settings

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

/** Sample Section ************
  <configSections>
    <section name="AuthorizedIPSettings" type="Company.Data.Service.AuthorizedIPSettings, Company.Data.Service"/>
  </configSections>
  <AuthorizedIPSettings>
    <AuthorizedIP>
      <add ip="10.10.3.44" clientName="My First Client" />
      <add ip="10.10.3.83" clientName="My Denied Client" allowed="false" />
    </AuthorizedIP>
  </AuthorizedIPSettings>
******************************/

namespace Company.Data.Service
{
    /// <summary>
    /// AuthorizedIPSettings (AuthorizedIP Node) Element 
    /// </summary>
    public class AuthorizedIP : ConfigurationElement
    {
        [ConfigurationProperty("ip", IsKey = true, IsRequired = true)]
        public string IP
        {
            get { return (string)this["ip"]; }
            set { this["ip"] = value; }
        }

        [ConfigurationProperty("clientName", DefaultValue = "")]
        public string ClientName
        {
            get { return (string)this["clientName"]; }
            set { this["clientName"] = value; }
        }

        [ConfigurationProperty("allowed", DefaultValue = true)]
        public bool Allowed
        {
            get { return (bool)this["allowed"]; }
            set { this["allowed"] = value; }
        }
    }

    /// <summary>
    /// AuthorizedIPSettings (all AuthorizedIP Nodes) Collection 
    /// </summary>
    [ConfigurationCollection(typeof(AuthorizedIP))]
    public class AuthorizedIPCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new AuthorizedIP();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((AuthorizedIP)(element)).IP;
        }

        public AuthorizedIP this[int idx]
        {
            get
            {
                return (AuthorizedIP)BaseGet(idx);
            }
        }

    }

    // AuthorizedIPSettings section = (AuthorizedIPSettings)ConfigurationManager.GetSection( "AuthorizedIPSettings" );
    /// <summary>
    /// Custom ConfigurationSection definition for AuthorizedIPSettings node
    /// </summary>
    public class AuthorizedIPSettings : ConfigurationSection
    {
        [ConfigurationProperty("AuthorizedIP")]
        public AuthorizedIPCollection AuthorizedIPItems
        {
            get { return ((AuthorizedIPCollection)(base["AuthorizedIP"])); }
        }
    }

}