gpt4 book ai didi

c# - 如何仅在 App_Data asp.net mvc 中允许下载访问特定文件夹

转载 作者:太空狗 更新时间:2023-10-29 22:13:51 24 4
gpt4 key购买 nike

我的 web.config 中有这个:

<system.webServer>
<security>
<requestFiltering>
<hiddenSegments>
<add segment="UserFiles"/>
</hiddenSegments>
</requestFiltering>
</security>
</system.webServer>

我认为我的处理方式是错误的,但我似乎找不到正确的方法来谷歌搜索。我只想授予对“UserFiles”文件夹的下载访问权限。我需要通过 web.config 执行此操作,因为实时环境将位于 Azure 上,因此我不会通过 RDP 来更改 IIS 的计算机。

最佳答案

首先,如果您使用的是 Azure Web 角色而不是 Azure 网站,则应该将这些内容存储在 blob 中。其次,是否需要保护这些文件,以便只有经过身份验证的用户才能访问它们(或者甚至用户只能访问自己的文件?)。

假设任何人都可以从服务器下载任何文件。如果是这种情况,请在内容下创建一个名为 UserFiles 的目录。现在,您可以像这样简单地链接到这些文件

<a href="@Url.Content("~\Content\UserFiles\filename.ext")">MY File title</a>

现在,如果它们受到身份验证方案的保护,事情就会变得棘手。您不希望任何人都能够下载这些项目。因此,让我们采取一些步骤来保护它们。

1.在解决方案的顶层创建一个名为 UserFiles 的文件夹。

2.在您的 web.config 中,将其设置为无人可以访问的位置

 <system.webServer>
<security>
<requestFiltering>
<hiddenSegments>
<add segment="UserFiles"/>
</hiddenSegments>
</requestFiltering>
</security>

3.创建一个 MVC Controller (我们称之为文件),您实际上将使用它来将文件传递给用户。在这里,我们进行一个名为“下载”的操作,该操作接收文件 ID(假设您将文件信息存储在数据库中)

public FileResult Download(int id){
//perform logic to see if user has access to this file
//if access, return the file
//else return a 404
}

现在,您的文件下载链接将如下所示

@Html.ActionLink("My File Title", "Download", "Files", new{id = Model.Id})

MVC 和您的代码将有权访问 UserData 文件夹,而外部 Web 用户则无权访问。使用 Controller /操作来控制您的内容

关于c# - 如何仅在 App_Data asp.net mvc 中允许下载访问特定文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21688116/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com