【问题标题】:Where to put JSON file with API key for Private Access将带有私有访问的 API 密钥的 JSON 文件放在哪里
【发布时间】:2020-03-27 06:11:16
【问题描述】:

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

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

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

【问题讨论】:

    标签: c# .net iis asmx


    【解决方案1】:

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

    【讨论】:

    • 我创建了App_Data 文件夹并将文件放在那里。我不断收到文件未找到异常string htmlFilePath = AppDomain.CurrentDomain.BaseDirectory; string contents = File.ReadAllText(htmlFilePath+@"\file.json");
    • 提及文件的真实路径。
    • HttpContext.Current.ApplicationInstance.Server.MapPath("~/App_Data")
    【解决方案2】:

    在 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 并从那里加载文件。

    【讨论】:

      猜你喜欢
      • 2013-06-02
      • 2016-05-13
      • 1970-01-01
      • 2013-08-26
      • 2012-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-06
      相关资源
      最近更新 更多