如何直接阻止访问内容?

时间:2013-04-16 17:14:20

标签: c# asp.net-mvc

使用ASP MVC 4.5,如何应用安全措施以防止用户直接访问内容?

例如,只需输入链接即可阻止访问存储在Web服务器上的图像或其他文件。

4 个答案:

答案 0 :(得分:5)

  1. 将您的图片放在非网络可访问的文件夹中。
  2. 创建一个服务器端脚本(例如,HttpHandler),它可以读取图像文件并在HTTP响应中返回其内容。
  3. 在该脚本中,执行用户验证以确保用户有权访问该文件。
  4. 在HTML中,src标记的img属性指向您的脚本。
  5. 用户仍然可以直接输入脚本的URL来查看图像。但您至少可以要求用户登录您的站点并有权查看该图像。

答案 1 :(得分:1)

使用ASP .NET Membership等身份验证系统,并需要某些凭据才能访问内容。除此之外,真的没有办法。如果用户具有直接链接并访问您网站的该区域,则根据Web服务器的工作方式,无法阻止它。

您可以采取某些安全措施来帮助阻止用户获取直接链接,但一个简单的方法就是禁用右键。

答案 2 :(得分:0)

除非您想要破坏用户,否则几乎无法做到。一种可能(并且广泛使用)的东西可能是检查您的推荐人(并使其在您的应用程序中),但这很容易被欺骗。

如果安全性是至关重要的,那么唯一要记住的是通过脚本下载所有内容来检查凭据(或者您可能需要的任何其他安全措施),但是您无法做其他事情。

如果浏览器确实已将某些内容下载到本地计算机,则绝对无法阻止该用户使用该数据(您可以设置一些障碍,例如避免右键单击等,但所有这些都可以以某种方式避免。)

答案 3 :(得分:0)

我已经制作了以下 HTTPHandler 以防止热链接。

它似乎适用于我的项目,但我当然不知道这是否是最佳做法。

    public void ProcessRequest(HttpContext context)
    {
        //write your handler implementation here.

        //Http
        HttpRequest request = context.Request;
        HttpResponse response = context.Response;

        //Header - Properites
        int Index = -1;
        string[] Keys = request.Headers.AllKeys;
        List<string[]> Values = new List<string[]>();

        //Header - Loop to get key values
        for (int i = 0; i < Keys.Length; i++)
        {
            Values.Add(request.Headers.GetValues(i));
            //Check if property "Accept" exists
            if (Keys[i].CompareTo("Accept") == 0)
                Index = i;
        }

        //Check if URL and URL Referrer are null
        if (context.Request.Url != null && context.Request.UrlReferrer != null && Index >= 0)
        {
            //Check image types
            if (!context.Request.UrlReferrer.AbsolutePath.EndsWith(".bmp") ||
                !context.Request.UrlReferrer.AbsolutePath.EndsWith(".jpg") ||
                !context.Request.UrlReferrer.AbsolutePath.EndsWith(".jpeg") ||
                !context.Request.UrlReferrer.AbsolutePath.EndsWith(".png"))
            {

                //Check header "Accept"
                if (Values[Index][0].CompareTo("*/*") == 0)
                {
                    //Get bytes from file
                    byte[] MyBytes = File.ReadAllBytes(context.Request.PhysicalPath);
                    //new HttpContext(context.Request, context.Response).Request.MapPath(context.Request.RawUrl).ToString()

                    context.Response.OutputStream.Write(MyBytes, 0, MyBytes.Length);

                    context.Response.Flush();
                }
                else
                    //Redirect                
                    context.Response.Redirect("/Home");

            }
            else
                //Redirect                
                context.Response.Redirect("/Home");
        }
        else
            //Redirect                
            context.Response.Redirect("/Home");
    }

Web.config 的修改方式如下:

<system.webServer>
    <handlers>
        <!--My-->
        <add name="PhotoHandler-BMP" path="*.bmp" verb="GET" type="MVCWebApplication.Handlers.PhotoHandler" resourceType="File" />
        <add name="PhotoHandler-JPG" path="*.jpg" verb="GET" type="MVCWebApplication.Handlers.PhotoHandler" resourceType="File" />
        <add name="PhotoHandler-JPEG" path="*.jpeg" verb="GET" type="MVCWebApplication.Handlers.PhotoHandler" resourceType="File" />
        <add name="PhotoHandler-PNG" path="*.png" verb="GET" type="MVCWebApplication.Handlers.PhotoHandler" resourceType="File" />
    </handlers>
</system.webServer>

随意评论任何改进。

相关问题