2013-03-22

Restrict user access to a website based on IP address

Though the method I’m going to describe below would not work 100%, it is the closest thing you can do within few minutes.

First, you may need to keep track of the suspected IP list in a database table or in some other storage. At the Application_BeginRequest method of the Global.asax file, iterate through each IP and match it with the current user’s IP. If it matches, then redirect the user to a permission denied page.

protected void Application_BeginRequest(object sender, EventArgs e)
{
    string ip = HttpContext.Current.Request.UserHostAddress;

    if(ip == suspectedIP){
        Response.Redirect("~/PermissionDenied.aspx");
    }
}

No comments: