1

我有一个 Web 应用程序 (asmx),它使用存储在 JSON 文件中的 API 机密。目前我已将文件放在 wwwroot/myapp/ 文件夹中

        string htmlFilePath = HttpContext.Current.Server.MapPath("~/");
        string contents = File.ReadAllText(htmlFilePath+@"\file.json");
        return contents;

此文件可从浏览器公开访问。如何使此文件只能由应用程序访问?

4

2 回答 2

2

在您的 Web 项目中创建一个名为 App_Data 的新文件夹,并将您的私有文件放在那里。默认情况下,ASP 将禁止公共访问 App_Data 文件夹中的文件。

于 2020-03-27T06:33:56.977 回答
0

在 IIS 的配置中,您可以使用过滤来拒绝访问服务器上的资源。如果我是对的,<denyUrlSequences>配置元素可能是合适的

<denyUrlSequences>元素包含一组元素,这些元素指定 IIS 将拒绝的 URL 字符序列,这有助于防止对 Web 服务器的基于 URL 的攻击。

在您的 IIS 配置中,使用以下内容过滤您的privatekey.json

<system.webServer>
   <security>
      <requestFiltering>
         <denyUrlSequences>
            <add sequence="privatekey.json" />
         </denyUrlSequences>
      </requestFiltering>
   </security>
</system.webServer>

否则,您可以将文件存储在 IIS 的根目录之外,例如C:\.private\privatekey.json并从那里加载文件。

于 2020-03-27T06:33:32.947 回答