Giter Site home page Giter Site logo

ip-address-filtering's Introduction

Build status

IP-Address-Filtering

Lightweight C# ASP.NET MVC address filtering library

The library allows validating IP address against:

  • Single IP address
  • List of multiple IP addresses
  • Single IP address range
  • Multiple IP address ranges
  • Supports CIDR IP address range format as well as IP4 subnets
  • Roles+IP list stored in updateable static class

How to use with the IP address hardcoded into attribute

	[IPAddressFilter("::1", IPAddressFilteringAction.Allow)]
	public ActionResult TestLocal()
	{
	    ViewBag.Message = "Local test page.";

	    return View();
	}
	[IPAddressFilter("192.168.0.100", IPAddressFilteringAction.Allow)]
	public ActionResult TestRemote()
	{
	    ViewBag.Message = "Remote test page.";

	    return View();
	}

Roles with IP list

First need to fill roles + IP list, do that on app start, use "0.0.0.0" to allow any IP

	var rolesWithIpList = new Dictionary<string, List<string>>()
	{
		{ "admin", new List<string>() {"192.168.10.100"}},
                { "user", new List<string>() {"192.168.10.200", "192.168.10.201", "192.168.10.202"}},
                { "simpleRange",new List<string>() { "8.8.8.7-8.8.8.9" } },
                { "netmask",new List<string>() { "192.168.80.1/255.255.255.0" } },
                { "prefix",new List<string>() { "192.168.9.1/24" } },
	};
	RolesContainer.UpdateRolesList(rolesWithIpList);

In controller mark methods with IPAddressRoleFilter attribute and specify roles that are allowed. Note that there are two predefined roles: "local" ("127.0.0.1", "::1") and "any" ("0.0.0.0")

	[IPAddressRoleFilter("admin,local")]
        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

Also the role named "global" is added by default to all IPAddressRoleFilter attributes This role allows to enable/disable IPs for all IPAddressRoleFilter attributes.

	[IPAddressRoleFilter("admin,local")]
	// same as
	[IPAddressRoleFilter("global,admin,local")]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.