Block IP addresses in Global.asax

Discussion in 'C#' started by homey, Aug 17, 2008.

  1. #1
    Im looking for a way to block specific IP addresses and net blocks from accessing all pages. I assume the best place to do this is the Global.asax page.

    I would love to do this in IIS but in a hosted environment this in not possible.

    Any ideas, code? suggestions?

    Thanks,
     
    homey, Aug 17, 2008 IP
  2. yugolancer

    yugolancer Well-Known Member

    Messages:
    320
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #2
    In the Session start - event handler:

    say you have an array of blocked IP's i.e.
    
    Dim bArr() As String = {"198.122.xxx.xx", "xxx.xxx.xx.xxx" etc.}
    
    Code (markup):

    
    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
    
    Code (markup):
    HTH
    Regards :)
     
    yugolancer, Aug 18, 2008 IP
  3. net-split

    net-split Peon

    Messages:
    29
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Have a look at this project at CodeGuru
    http://www.codeguru.com/csharp/.net/net_general/internet/article.php/c10651
     
    net-split, Aug 18, 2008 IP
  4. Spartan_Strategy

    Spartan_Strategy Peon

    Messages:
    197
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I was looking for this exact code only in PHP! Thanks net-split!
     
    Spartan_Strategy, Oct 6, 2008 IP