asp.net - 使用 ssl 和客户端证书上传大文件 (uploadReadAheadSize) 但不希望预读所有数据

标签 asp.net .net iis asp.net-mvc-5 asp.net-web-api2

我尝试搜索互联网/堆栈溢出,但找不到任何适合我的相关答案。

我有一个 asp.net web api2 应用程序(仅使用 ssl)。 我试图允许大文件上传(最多 ~36mb),但除非我将 uploadReadAheadSize 更改为预读此大小,否则我会从 IIS 收到错误,指出请求太长。

我不想设置该属性,因为这样我的数据流只有在 IIS 读取完所有内容后才会到达。

我的问题是:如何使用 ASP.NET 托管并使用 ssl(!) 和 Web api 2 进行大文件上传,而无需配置大的 uploadreadaheadsize?因为我采取的所有步骤似乎都不够。

uploadReadAheadSize:

Specifies the number of bytes that a Web server will read into a buffer and pass to an ISAPI extension or module. This occurs once per client request. The ISAPI extension or module receives any additional data directly from the client. The value must be between 0 and 2147483647. The default value is 49152.

如果我不设置 uploadReadAheadSize 并尝试通过 ssl 上传大文件,则会出现以下错误:

HTTP Error 413.0 - Request Entity Too Large

Things you can try: The page was not displayed because the request entity is too large.

Most likely causes:

The Web server is refusing to service the request because the request entity is too large. The Web server cannot service the request because it is trying to negotiate a client certificate but the request entity is too large. The request URL or the physical mapping to the URL (i.e., the physical file system path to the URL's content) is too long. Things you can try: Verify that the request is valid. If using client certificates, try:

Increasing system.webServer/serverRuntime@uploadReadAheadSize

Configure your SSL endpoint to negotiate client certificates as part of the initial SSL handshake. (netsh http add sslcert ... clientcertnegotiation=enable)

Verify that the request is valid.
  If using client certificates, try:
    Increasing system.webServer/serverRuntime@uploadReadAheadSize
    Configure your SSL endpoint to negotiate client certificates as part of the initial SSL handshake. (netsh http add sslcert ... clientcertnegotiation=enable)

如果我将预读配置为我想要允许的大小,iis 会允许我的请求:

C:\Windows\SysWOW64>C:\Windows\System32\inetsrv\appcmd set config ... -section:system.webServer/serverRuntime /uploadReadAheadSize:n /commit:apphost
Applied configuration changes to section "system.webServer/serverRuntime" for "MACHINE/WEBROOT/APPHOST/..." at configuration commit path "MACHINE/WEBROOT/APPHOST"

我在 web.config 中进行了配置:

...
<httpRuntime targetFramework="4.5.1" maxRequestLength="36864"  /> 
...

...
<system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="37748736"> 
...

我有一个接收文件的虚拟 Controller

[ControllerBufferlessAttribute()]
public class UploadController : ApiController
{
    public HttpResponseMessage Post([FromUri]string filename)
    {
        var selector = RequestContext.Configuration.Services.GetHostBufferPolicySelector();
            var isbufferless = string.Format("It is {0} that i'm bufferless but still IIS already read all the data before this context because of the readahead", selector is PolicySelectorBufferless);
            return new HttpResponseMessage(HttpStatusCode.InternalServerError) { ReasonPhrase = isbufferless };          
    }
}

属性和策略:

public class ControllerBufferlessAttribute : Attribute, IControllerConfiguration
{
    public void Initialize(HttpControllerSettings settings,
        HttpControllerDescriptor descriptor)
    {
        settings.Services.Replace(typeof(IHostBufferPolicySelector), new PolicySelectorBufferless());
    }
}
public class PolicySelectorBufferless : WebHostBufferPolicySelector
{
    public override bool UseBufferedInputStream(object hostContext)
    {
        return false;
    }
    public override bool UseBufferedOutputStream(HttpResponseMessage response)
    {
        return base.UseBufferedOutputStream(response);
    }
}

我在请求中获得的内容

It is True that i'm bufferless but still IIS already read all the data before this context because of the readahead

最佳答案

为了使用 requestLimits 功能,您需要 set it up on IIS级别第一。

也许您一开始就忘记在 IIS 服务器上安装请求过滤。

HTH

关于asp.net - 使用 ssl 和客户端证书上传大文件 (uploadReadAheadSize) 但不希望预读所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20973540/

相关文章:

asp.net - 防止 FormsAuthenticationModule 拦截 ASP.NET Web API 响应

C# 文件位置难题

asp.net - CRM 2015 Linq Count() 查询 - 这是在枚举查询吗?

从 Windows 7 Unlimited 升级到 Windows 10 Pro 后 IIS 不工作

asp.net-mvc-4 - 有了 AngularJs + Web API,为什么还需要 ASP.NET MVC 和 IIS?

c# - 在 ASP.NET (C#) 中创建动态 RSS 提要页面 - 我需要做任何额外的事情吗?

javascript - 将参数传递给 asp.net 页面中的 javascript 函数

java - .Net 和 Java 接口(interface)是否足够相似,可以以某种方式进行整合?

.net - 我可以将 .NET 网站复制/粘贴到 Windows Azure 吗?

c# - 当构建 ".Net Standard"项目时 - 这意味着什么?