【问题标题】:ASP.NET Core 2.2 Upgrade - IIS: limit request content lengthASP.NET Core 2.2 升级 - IIS:限制请求内容长度
【发布时间】:2020-02-13 11:40:20
【问题描述】:

直到现在,我们一直在 ASP.NET Core 1.1 上运行我们的服务。我们刚刚升级到 ASP.NET Core 2.2,非常顺利。

但是,我们在 Windows 上的 Azure App Service 上托管,而后者似乎又运行 IIS。

现在我们在web.config 中有一个自定义部分来限制最大内容长度,因此当用户上传文件时,如果他们的上传失败,他们在实际上传之前就知道限制:

<?xml version="1.0" encoding="utf-8"?>
<configuration>    
    <location path="api/0.1/files">
        <system.web>
            <httpRuntime maxRequestLength="409600" executionTimeout="3600"/>
        </system.web>

        <system.webServer>
            <security>
                <requestFiltering>

                    <requestLimits maxAllowedContentLength="419430400" />
                </requestFiltering>
            </security>
        </system.webServer>
    </location>

  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore"
             path="*"
             verb="*"
             modules="AspNetCoreModule"
             resourceType="Unspecified"/>
      </handlers>
      <aspNetCore processPath="dotnet"
                  arguments=".\webapp.dll"
                  stdoutLogEnabled="false"
                  stdoutLogFile="\\?\%home%\LogFiles\stdout"
                  hostingModel="OutOfProcess"
                  forwardWindowsAuthToken="false"/>
    </system.webServer>
  </location>

</configuration>

现在,调用路由 api/0.1/files(当然还有所有“位于”文件下的路由)将产生 404 not found 结果,并显示以下错误消息:

您要查找的资源已被删除、更改名称或暂时不可用。

我能找到的唯一解决方法是全局限制内容长度:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore"
             path="*"
             verb="*"
             modules="AspNetCoreModule"
             resourceType="Unspecified"/>
      </handlers>
      <aspNetCore processPath="dotnet"
                  arguments=".\webapp.dll"
                  stdoutLogEnabled="false"
                  stdoutLogFile="\\?\%home%\LogFiles\stdout"
                  hostingModel="OutOfProcess"
                  forwardWindowsAuthToken="false"/>

      <security>
        <requestFiltering>
          <!--400 MByte-->
          <requestLimits maxAllowedContentLength="419430400" />
        </requestFiltering>
      </security>
    </system.webServer>
  </location>

</configuration>

为特定路线设置限制的正确方法是什么?

【问题讨论】:

  • 您是否尝试在您的操作中使用[RequestSizeLimit(...)]?更多详情请参考document

标签: c# asp.net-core iis azure-web-app-service asp.net-core-2.2


【解决方案1】:

可以使用location设置特定路由的限制。

此外,如果您想更改特定 MVC 操作或控制器的最大请求正文大小限制,您可以使用 RequestSizeLimit 属性。

// GET api/values/5
[HttpGet("{id}")]
[RequestSizeLimit(40000000)]
public ActionResult<string> Get(int id)
{
    return "value";
}

设置此操作方法允许的最大请求长度。您可以在操作级别或控制器级别应用此属性。这是增加 ASP.NET Core MVC 应用程序限制的推荐方法。

【讨论】:

  • 我可以接受这个作为答案,但这只是故事的一半:这有效,但需要将web.config 中的最大请求长度全局设置为等于或大于您实际需要的最大请求长度任何地方在您的应用程序中。所以这并不是我们真正想要的,因为我们不希望 IIS 上的其他路由接受大于 30MB 的请求,尽管之后它们会被 Kestrel 丢弃。
猜你喜欢
  • 2020-03-13
  • 2020-01-06
  • 2020-01-29
  • 2019-05-08
  • 1970-01-01
  • 2012-05-07
  • 1970-01-01
  • 1970-01-01
  • 2020-03-22
相关资源
最近更新 更多