Restrict direct file access of our attachment folder

倖福魔咒の 提交于 2019-12-25 00:59:11

问题


We have this website where we also have a visa application module. The visa application module requires the user to create an account. Whenever the user uploads attachments it is saved in a specific folder inside attachments. This specific folder corresponds to a uniquely generated numbers that is assigned to each user.

Now what I need is if the user is not login they should not be able to access the files inside the attachment folders.

How can I achieve that using http handler?

I have this code

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();
    }

But the problem is I'm having a problem configuring it in web config.

Does anybody knows how to do this?


回答1:


Take a look at one of my answers

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

This rule is actualy applied to folders, just what are you looking for.



来源:https://stackoverflow.com/questions/13914846/restrict-direct-file-access-of-our-attachment-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!