2009-12-22 48 views
0

登入我需要确保获得在.NET Web应用程序的所有页面 - 除了从请求:ASP.NET:需要根据子网掩码

  • 本地网络(网络IIS上运行)
  • IP地址在数据库中列出的上市/掩码

所有其他requesets应该被重定向到一个登录表单

我是在一个HTTP模块的方向思考 - 但从来没有写过一个。 任何人都可以提供任何想法吗?

谢谢!

+0

我建议您应该在防火墙中执行此操作如果您确实有权访问您的服务器防火墙。 – 2009-12-22 10:12:44

回答

0

使用HttpModule将是最好的方法。您可以使用它在页面执行前捕获任何请求,并在需要时重定向到登录表单。

public class SecurityModule : IHttpModule 
{ 
    private HttpApplication m_HttpApplication; 

    public void Init(HttpApplication context) 
    { 
     m_HttpApplication = context; 
     m_HttpApplication.PreRequestHandlerExecute += new EventHandler(OnPreRequestHandlerExecute); 
    } 

    public void Dispose() 
    { 
     // Do Nothing 
    } 

    private void OnPreRequestHandlerExecute(object sender, EventArgs e) 
    { 
     // Get IP address 
     string ipAddress = m_HttpApplication.Context.Request.UserHostAddress; 

     // Check if the IP address requires login 
     bool requiresLogin = ValidateIpAddress(ipAddress); 

     // Redirect if required 
     if (requiresLogin) 
      Response.Redirect("~/Login.aspx", true); 
     } 

     private bool ValidateIpAddress(string ipAddress) 
     { 
      // This method would check that the IP address is from the local 
      // network or in the database and return true or false accordingly. 

      return false; 
     } 
    } 

您还需要修改web.config文件并添加引用到模块:

<httpModules> 
    <add name="SecurityModule" type="MyApp.SecurityModule, MyApp"/> 
</httpModules> 

此代码也需要做一些修改,以确保在已登录的用户谁不重定向回到登录页面,但它应该足以让你开始。

0

我宁愿建立一个全球认证方法来检查IP。在你的MasterPage的OnInit或OnLoad中调用这个函数或者你自己的System.Web.Page的实现应该可以做到。

如果用户必须登录,请在会话中设置一些随机生成的ID来检查(将随机ID保存到数据库和会话中)。在您的全局身份验证方法中,您现在可以检查有效的IP范围或有效的(数据库注册的)会话令牌。