限制我们的附件文件夹的直接文件访问

时间:2012-12-17 13:10:55

标签: c# asp.net iis-7 web-config generic-handler

我们有这个网站,我们也有签证申请模块。签证申请模块要求用户创建一个帐户。每当用户上传附件时,它都会保存在附件内的特定文件夹中。此特定文件夹对应于分配给每个用户的唯一生成的数字。

现在我需要的是如果用户没有登录,他们应该无法访问附件文件夹中的文件。

如何使用http处理程序实现这一目标?

我有这段代码

if (HttpContext.Current.Session["UserRegistrationID"] != null)
            {
                if (HttpContext.Current.Session["UserRegistrationID"].ToString() != "")
                {
                    DownloadFile(context);
                }
                else
                {
                    context.Response.Write("You don't have the rights to access this file.");
                    context.Response.Flush();
                }
            }
            else
            {
                context.Response.Write("You don't have the rights to access this file.");
                context.Response.Flush();
            }

protected void DownloadFile(HttpContext context)
        {

        context.Response.ContentType = "image/jpg";
        context.Response.WriteFile(context.Request.PhysicalPath);
        context.Response.Flush();
    }

但问题是我在网络配置中配置它时遇到问题。

有人知道怎么做吗?

1 个答案:

答案 0 :(得分:0)

看看我的一个答案

How to restrict unlogged unauthorized users from viewing web pages in asp net

这条规则实际上适用于文件夹,只是你在寻找什么。

相关问题