如何通过HTTP访问.html文件,但只能通过重定向?

时间:2014-05-15 22:19:44

标签: html asp.net security networking

我们的服务器上有一个可以通过URL直接访问的文件,但此时存在安全问题。

我们的系统会在弹出窗口中打开文件,但您也可以通过直接导航到其网址直接访问该页面。

我们如何防止这种情况,只允许通过重定向访问文件?

1 个答案:

答案 0 :(得分:3)

在打开弹出窗口的页面上设置Session变量:

Session["MainPageVisited"] = true;

在弹出页面上检查以下值:

if (Session["MainPageVisited"] == null || !Session["MainPageVisited"]) 
{
  Response.Redirect("http://www.example.com/", true);
}

要使此解决方案正常运行,您的html文件需要作为aspx投放。或者,您可以创建一个HTTP模块,如果您需要它是实际的html

创建模块

using System;
using System.Web;
public class HelloWorldModule : IHttpModule
{
    public HelloWorldModule()
    {
    }

    public String ModuleName
    {
        get { return "HelloWorldModule"; }
    }

    // In the Init function, register for HttpApplication 
    // events by adding your handlers.
    public void Init(HttpApplication application)
    {
        application.BeginRequest += 
            (new EventHandler(this.Application_BeginRequest));
    }

    private void Application_BeginRequest(Object source, 
         EventArgs e)
    {
    // Create HttpApplication and HttpContext objects to access
    // request and response properties.
        HttpApplication application = (HttpApplication)source;
        HttpContext context = application.Context;
        string filePath = context.Request.FilePath;
        string fileExtension = 
            VirtualPathUtility.GetExtension(filePath);
        if (fileExtension.Equals(".html"))
        {
            if (Session["MainPageVisited"] == null || !Session["MainPageVisited"]) 
            {
              // Handle it
            }
        }
    }

    public void Dispose() { }
}

注册以经典模式运行的IIS 6.0和IIS 7.0的模块

<configuration>
  <system.web>
    <httpModules>
      <add name="HelloWorldModule" type="HelloWorldModule"/>
     </httpModules>
  </system.web>
</configuration>

注册在集成模式下运行的IIS 7.0模块

<configuration>
  <system.webServer>
    <modules>
      <add name="HelloWorldModule" type="HelloWorldModule"/>
    </modules>
  </system.webServer>
</configuration>

请注意,这是在未经测试的情况下创建的,但它应该让您走上正确的轨道。确保通过ASP.NET映射所有请求以使其正常工作(集成模式或设置通配符应用程序映射)。