Nithin Viswanathan
Nithin Viswanathan

Reputation: 3283

Blocking list of IP addresses in ASP.NET web application/website

I have a group of IP addresses.

After deploying my application, I want to only be able to access my application from a particular IP address.

How can I achieve this using the Global.asax (not through IIS)?

Upvotes: 5

Views: 1925

Answers (3)

Nathan
Nathan

Reputation: 6216

This is a good starting point for you

(especially as it's separated nicely into a HttpModule for subsequent re-use)

Upvotes: 2

Davide Piras
Davide Piras

Reputation: 44595

I would start in this way, in the begin request event handler in your Global class, I would determine the client IP address following this answer: https://stackoverflow.com/a/9567439/559144

then if the connecting ip is not in the allowed list, I would redirect to another page like an access denied page, a login page or the company / google home page.

Upvotes: 0

Varun
Varun

Reputation: 373

In the Session start - event handler:

say you have an array of blocked IP's i.e. Code (text):

Dim bArr() As String = {"198.122.xxx.xx", "xxx.xxx.xx.xxx" etc.}

Code (text):

Dim strIP = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If strIP="" Then strIP = Request.ServerVariables("REMOTE_ADDR")

For i As integer = 0 To bArr.UperBound
  If strIP = bArr(i) Then
     Response.Redirect("Permissionsdenied.html")
  End If
Next

Upvotes: 0

Related Questions