2011-06-02 64 views
0

是否可以設置我的entaire Web應用程序只能從給定的IP地址?我可以使用global.asax或某些東西來將代碼放在一個地方,並能夠自由地刪除它嗎?使網站只能從給定的IP

感謝任何提示

+0

<system.webServer>標籤做你想要throught只有一個IP站點的訪問? – Saurabh 2011-06-02 10:11:44

+0

是,僅用於測試目的,如果ip不同,則重定向到另一個站點。我不能公佈網站上的產品,但我想測試環境 – gruber 2011-06-02 10:15:24

回答

2

最好的解決辦法是在IIS中過濾IP地址。
我已經做到了,它能正常工作......並且您不必更改一行代碼。

如果您沒有訪問IIS,那麼你就可以按照斯科特Hanselman的suggestion,並創建一個自定義HttpModule

namespace YourModuleNameHere 
{ 
    public class IPBlackList : IHttpModule 
    { 
     private EventHandler onBeginRequest; 

     public IPBlackList() 
     { 
      onBeginRequest = new EventHandler(this.HandleBeginRequest); 
     } 

     void IHttpModule.Dispose() 
     { 
     } 

     void IHttpModule.Init(HttpApplication context) 
     { 
      context.BeginRequest += onBeginRequest; 
     } 

     const string BLOCKEDIPSKEY = "blockedips"; 
     const string BLOCKEDIPSFILE = "SiteConfig/blockedips.config"; 

     public static StringDictionary GetBlockedIPs(HttpContext context) 
     { 
      StringDictionary ips = (StringDictionary)context.Cache[BLOCKEDIPSKEY]; 
      if (ips == null) 
      { 
       ips = GetBlockedIPs(GetBlockedIPsFilePathFromCurrentContext(context)); 
       context.Cache.Insert(BLOCKEDIPSKEY, ips, new CacheDependency(GetBlockedIPsFilePathFromCurrentContext(context))); 
      } 
      return ips; 
     } 

     private static string BlockedIPFileName = null; 
     private static object blockedIPFileNameObject = new object(); 
     public static string GetBlockedIPsFilePathFromCurrentContext(HttpContext context) 
     { 
      if (BlockedIPFileName != null) 
       return BlockedIPFileName; 
      lock (blockedIPFileNameObject) 
      { 
       if (BlockedIPFileName == null) 
       { 
        BlockedIPFileName = context.Server.MapPath(BLOCKEDIPSFILE); 
       } 
      } 
      return BlockedIPFileName; 
     } 

     public static StringDictionary GetBlockedIPs(string configPath) 
     { 
      StringDictionary retval = new StringDictionary(); 
      using (StreamReader sr = new StreamReader(configPath)) 
      { 
       String line; 
       while ((line = sr.ReadLine()) != null) 
       { 
        line = line.Trim(); 
        if (line.Length != 0) 
        { 
         retval.Add(line, null); 
        } 
       } 
      } 
      return retval; 
     } 

     private void HandleBeginRequest(object sender, EventArgs evargs) 
     { 
      HttpApplication app = sender as HttpApplication; 

      if (app != null) 
      { 
       string IPAddr = app.Context.Request.ServerVariables["REMOTE_ADDR"]; 
       if (IPAddr == null || IPAddr.Length == 0) 
       { 
        return; 
       } 

       StringDictionary badIPs = GetBlockedIPs(app.Context); 
       if (badIPs != null && badIPs.ContainsKey(IPAddr)) 
       { 
        app.Context.Response.StatusCode = 404; 
        app.Context.Response.SuppressContent = true; 
        app.Context.Response.End(); 
        return; 
       } 
      } 
     } 
    } 
} 

,並在使用它您web.config

<system.web> 
    <httpModules> 
     <add type = "YourModuleNameHere.IPBlackList, YourAssemblyNameHere" name="IPBlackList" /> 
    </httpModules> 
</system.web> 
+0

我沒有訪問iis – gruber 2011-06-02 10:12:31

+0

@gruber:我已經擴展了我的答案。 – LeftyX 2011-06-02 10:15:34

0

是的,它是可能。您可以從訪問它的位置獲取系統IP地址並可以阻止。

Request.Params [ 「REMOTE_ADDR」]

你可以看到這個鏈接的詳細信息

Best way to restrict access by IP address?

+0

但是在哪種情況下,我應該檢查並阻止? – gruber 2011-06-02 10:13:27

+1

你可以使用application_beginrequest – AjayR 2011-06-02 11:15:55

0

如果您沒有訪問到IIS,或者您需要從ASP控制它。網,你可以的BeginRequest檢查REMOTE_HOST或REMOTE_ADDR

protected void Application_BeginRequest(Object sender, EventArgs e) 
{ 
    HttpApplication app = (HttpApplication)sender; 

    if(app.Request.ServerVariables["REMOTE_ADDR"] != "1.1.1.1") 
    { 
     HttpContext.Current.Response.End(); 
     return 
    } 
} 

,但你還必須考慮Ip spoofing

PS:REMOTE_HOSTREMOTE_ADDR回我總是IP而已,可能是因爲IIS需要一些額外的設置對主機parametre獲得地址

0

你可以創建爲和比HTTP模塊如果您無法訪問您的IIS,請在web.config中註冊它。

HttpModule結構應該如下所示;

namespace MyApp { 

    public class MyModule : IHttpModule { 

     public void Init(HttpApplication context) { 

     } 

     public void Dispose() { 

     } 

    } 
} 

您實現Init事件中你的邏輯後,您需要註冊的web.config文件中的模塊,以執行它在每次請求;

<configuration> 
    <system.web> 
     <httpModules> 
     <add name="MyModule" type="MyApp.MyModule, MyApp" /> 
     </httpModules> 
    </system.web> 
</configuration> 

如果您在IIS 7或7.5的集成模式,該登記應當裏面的web.config

0
protected void Application_BeginRequest(object sender, EventArgs e) 
    { 
     string ip = Request.Params["REMOTE_ADDR"].ToString(); 
     if (ip == "your-ip") 
     { 
      // no action 
     } 
     else 
     { 
      Response.Redirect("url"); 
     } 
    }