{"id":74,"date":"2019-09-15T05:56:48","date_gmt":"2019-09-15T05:56:48","guid":{"rendered":"http:\/\/sumitjangid.com\/?p=74"},"modified":"2019-09-15T05:56:48","modified_gmt":"2019-09-15T05:56:48","slug":"creating-ip-based-access-validation","status":"publish","type":"post","link":"http:\/\/sumitjangid.com\/index.php\/2019\/09\/15\/creating-ip-based-access-validation\/","title":{"rendered":"Creating IP based Access Validation"},"content":{"rendered":"\n<p>First you have to create a validation class which will do the work.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class AuthorizeIPAddressAttribute : ActionFilterAttribute\n    {\n        public override void OnActionExecuting(ActionExecutingContext context)\n        {\n            string ipAddress = HttpContext.Current.Request.UserHostAddress;\n            \n            if (!IsIpAddressAllowed(ipAddress.Trim()))\n            {\n                using (OPIEntities OPIdb = new OPIEntities())\n                {\n                    activity act = new activity();\n                    act.activityuser = \"user\";\n                    act.message = \"Unauthorised IP Host Address: \"+ipAddress+\" for \"+HttpContext.Current.Request.Url;\n                    act.type = \"actype\";\n                    act.activitytime = DateTime.Now.AddHours(4);\n                    act.IpAddress = ipAddress;\n                    OPIdb.activities.Add(act);\n                    OPIdb.SaveChanges();\n                }\n                    context.Result = new HttpStatusCodeResult(403);\n            }\n            base.OnActionExecuting(context);\n        }\n\n        private bool IsIpAddressAllowed(string IpAddress)\n        {\n            if (!string.IsNullOrWhiteSpace(IpAddress))\n            {\n                string[] addresses = Convert.ToString(ConfigurationManager.AppSettings[\"AllowedIPAddresses\"]).Split(',');\n                return addresses.Where(a => a.Trim().Equals(IpAddress, StringComparison.InvariantCultureIgnoreCase)).Any();\n            }\n            return false;\n        }\n    }\n\n<\/code><\/pre>\n\n\n\n<p>Once that is done, now you can use the attributes in your controllers and actions like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>[AuthorizeIPAddress]\npublic class HomeController : Controller  {}\nor\n[AuthorizeIPAddress]\npublic ActionResult Index()<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>First you have to create a validation class which will do the work. Once that is done, now you can use the attributes in your controllers and actions like this:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,5,2],"tags":[],"_links":{"self":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/74"}],"collection":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/comments?post=74"}],"version-history":[{"count":1,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/74\/revisions"}],"predecessor-version":[{"id":75,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/74\/revisions\/75"}],"wp:attachment":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/media?parent=74"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/categories?post=74"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/tags?post=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}