简体   繁体   English

阻止用户访问文件,但允许管理员访问 asp.net IIS C#

[英]block access to files for users but allow access for admins asp.net IIS C#

I have a website users can create files that are stored for them as PDF's.我有一个网站,用户可以创建为他们存储为 PDF 的文件。 I want to block direct access to those files for the general user but still allow direct access for the admin.我想阻止普通用户直接访问这些文件,但仍然允许管理员直接访问。 So that random users cannot just type in something like www.website.com/files/hello.pdf and gain access to any user's files.因此,随机用户不能只输入www.website.com/files/hello.pdf之类的内容并访问任何用户的文件。 But I want the admin to be able to access that file.但我希望管理员能够访问该文件。

I have tried Hidden segments in IIS but that blocks the entire folder for everyone, including the admin.我已经尝试过 IIS 中的隐藏段,但这会阻止包括管理员在内的所有人的整个文件夹。 I have tried to figure out how to use request filtering rules but I just can't seem to get it right.我试图弄清楚如何使用请求过滤规则,但我似乎无法做到正确。

A user's role is stored as a Session[].用户的角色存储为 Session[]。 if that session is "userA" role then they are a general user and I do not want them to have direct access to the file.如果 session 是“userA”角色,那么他们是普通用户,我不希望他们直接访问该文件。 But if that session is "userB" role then they are an admin and I want them to have direct access to the file.但是,如果 session 是“userB”角色,那么他们是管理员,我希望他们能够直接访问该文件。 eg www.website.com\files\hello.pdf例如 www.website.com\files\hello.pdf

My other issue is that the PDF Files are within a GridView, so more than one file will be embedded into multiple rows.我的另一个问题是 PDF 文件位于 GridView 内,因此多个文件将嵌入到多行中。 I have figured out how to embed the PDF into the ASPX by converting the PDF Info to Base64 and then inserting that data into the source of the embed tag.我已经弄清楚如何通过将 PDF 信息转换为 Base64 然后将该数据插入到嵌入标签的源中来将 PDF 嵌入到 ASPX 中。 The issue now is that this is some heavy lifting for the browser when loading more than 10 rows from the GridView.现在的问题是,当从 GridView 加载超过 10 行时,这对浏览器来说是一些繁重的工作。 So it works but I imagine there is a more memory-efficient way of doing this.所以它有效,但我想有一种更节省内存的方式来做到这一点。

This is what I did so far这是我到目前为止所做的

<embed src='<%# Eval("pdfBytes")%>' type="application/pdf" width="910px" height="1100px"/>

 string FilePath = Server.MapPath(FileLocation);
                            WebClient User = new WebClient();
                            Byte[] FileBuffer = User.DownloadData((FilePath));
                            if (FileBuffer != null)
                            {
                                
                                base64PDF = "data:application/pdf;base64, " + System.Convert.ToBase64String(FileBuffer, 0, FileBuffer.Length);

                                dt.Rows[i]["pdfBytes"] = base64PDF + "  #>";


                            }


                        }

Any thoughts on how I can do this?关于我如何做到这一点的任何想法? Please help请帮忙

This is the steps that you have to follow.这是您必须遵循的步骤。

  1. You save your files under App_Data directory, that is inaccessible from any user.您将文件保存在App_Data目录下,任何用户都无法访问该目录。 Only pool can access any data there, so you can save and read there on code behind, but not direct access it by any url只有池可以访问那里的任何数据,因此您可以在后面的代码中保存和阅读,但不能通过任何 url 直接访问它
  2. You create a handler thats returns that file (base on some parameter that you send on url) if your user have this role.如果您的用户具有此角色,您将创建一个返回该文件的处理程序(基于您在 url 上发送的某些参数)。

eg, you create a download.ashx , make the checks if the user have the roles and its authenticated, add the correct headers and content type, disable the cache...例如,您创建一个download.ashx ,检查用户是否具有角色及其身份验证,添加正确的标题和内容类型,禁用缓存...

context.Response.ContentType = "application/pdf";
context.Response.AppendHeader("Content-disposition", "attachment; filename=" + FullFileName);   

and add some code to read and send the protected file to Response Output Stream.并添加一些代码来读取受保护的文件并将其发送到响应 Output Stream。

I could not find a clear answer on the web but with a lot of this communities help I was able to put this together as an answer to my problem.我无法在 web 上找到明确的答案,但在很多社区的帮助下,我能够将其放在一起作为我的问题的答案。

On the ASPX where I have my GridView I placed this code在我有 GridView 的 ASPX 上,我放置了此代码

<iframe id="iframePDF" runat="server"  width="910px" height="1100px" scrolling="no" frameborder="0" ></iframe>

In the ASPX.CS where I have my Gridview I filled the GridView from SQL and placed these codes into the GridViewRowEventHandler.在我有 Gridview 的 ASPX.CS 中,我从 SQL 中填充了 GridView 并将这些代码放入 GridViewRowEventHandler。

if (e.Row.RowType == DataControlRowType.DataRow)
        {

            using (SqlConnection con = new SqlConnection(strcon))
            {
                if (con.State == ConnectionState.Closed)

                {
                    con.Open();
                }

                Label flagID = (Label)e.Row.FindControl("ID");
                

                SqlCommand cmd = new SqlCommand("COMMAND ", con);

                cmd.CommandType = CommandType.Text;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);

                if (dt.Rows.Count >= 1)
                {

                    System.Web.UI.HtmlControls.HtmlIframe Iframe = (System.Web.UI.HtmlControls.HtmlIframe)e.Row.FindControl("IFRAMEID");

                    Iframe .Attributes["src"] = "fileViewer.aspx?Location=" + dt.Rows[0]["FileLocation"].ToString();
                }
                }
 }

Then in the FileViewer.ASPX I put an IFrame where the PDF will be viewed I pulled the SRC from the Query String I attached to the URL然后在 FileViewer.ASPX 中我放了一个 IFrame 将在其中查看 PDF 我从附加到 ZE6B391A8D2C4D45902A23A8B6585703D 的查询字符串中提取了 SRC

 <iframe width="910px" height="1100px" scrolling="no" frameborder="0" id="iframe"></iframe>

and in the FileViewer.ASPX.CS I placed the below code to fill the SRC of this new IFrame.在 FileViewer.ASPX.CS 中,我放置了以下代码来填充这个新 IFrame 的 SRC。 I run this on page load.我在页面加载时运行它。

 void FileLoader()
    {
        Response.ContentType = "application/pdf";
        Response.AppendHeader("Content-disposition", "inline; filename=" + "PDFFile");
        string test = Request.QueryString["Location"];
        Response.TransmitFile(Request.QueryString["Location"]);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET web.config表单身份验证,拒绝匿名用户,允许匿名访问单个文件 - ASP.NET web.config Forms Authentication, deny anonymous users, allow anonymous access for single files 试图允许用户上传文件; 文件为“空”(ASP.Net / MVC / C#) - Trying to allow users to upload files; File comes in “null” (ASP.Net / MVC / C#) ASP.NET C# 文件访问 - ASP.NET C# File Access 如何使用C#和ASP.NET重写URL而不访问IIS? - How do I rewrite URLs without access to IIS using C# and ASP.NET? 阻止访问 ASP.NET 中的 URL 和 Z2567A5EC9705EB7AC2C984033E06 文件中的 C#。 - Block Access to URL in ASP.NET and C# in web.config file 上传和下载文件 ASP.NET C# IIS7 - upload and download files ASP.NET C# IIS7 在ASP.Net中将静态文件限制为对经过身份验证的用户进行访问 - Restrict static files access to authenticated users in ASP.Net asp.net c#仅使用全局asax将文件夹访问限制为登录的用户 - asp.net c# Restrict a folder access to logged users only using global asax 如果有太多用户使用c#使用asp.net访问sql中的那个表,那么更新表的最佳方法是 - Best way to update table if too many users access that table in sql with asp.net using c# 如何在Asp.NET C#中访问客户端PC的C:\\ Program Files \\…路径? - How to access C:\Program Files\… path of client PC in Asp.NET C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM