将https重定向的IIS重写为http导致null httpcontext.request.contenttype导致



我正在使用具有applyspaildhost.xdt的https重定向站点扩展名:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <location path="%XDT_SITENAME%" xdt:Transform="InsertIfMissing" xdt:Locator="Match(path)">
        <system.webServer xdt:Transform="InsertIfMissing">
            <rewrite xdt:Transform="InsertIfMissing">
                <rules xdt:Transform="InsertIfMissing">
                    <rule name="redirect HTTP to HTTPS" enabled="true" stopProcessing="true" xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)">
                        <match url="(.*)" />
                        <conditions>
                            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                            <add input="{WARMUP_REQUEST}" pattern="1" negate="true" />
                        </conditions>
                        <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
                    </rule>
                </rules>
            </rewrite>
        </system.webServer>
    </location>
</configuration>

当Postman发送到HTTP而不是https时,我将获得null的CC_1,上面的生效。

i使用此中间件进行测试:

public class TestMiddleware
{
    readonly RequestDelegate _next;
    public TestMiddleware(RequestDelegate next)
    {
        if (next == null) throw new ArgumentNullException(nameof(next));
        _next = next;
    }
    public async Task Invoke(HttpContext httpContext)
    {
        if (httpContext == null)
        {
            throw new ArgumentNullException(nameof(httpContext));
        }
        var requestContentType = httpContext.Request.ContentType;
        await httpContext.Response.WriteAsync($"requestContentType: {requestContentType}");
    }
} 

我宁愿继续处理IIS中的HTTP重定向,而不是.Net Core的常见中间件解决方案。

有人知道要添加到 applicationhost.xdt的参数吗?

更新1 :为了清楚:

当我致电http并重定向到https时,此问题仅重现

相反,当我使用HTTPS调用时,我会得到预期的Request.ContentType结果。

使用以下答案中提供的解决方案,我得到相同的行为。我不再确定解决方案将是编辑 applicationhost.xdt。也许我需要以另一种方式获得Request.ContentType

请注意,现在有一种更简单的方法将HTTP流量重定向到HTTPS:在自定义域下,只需SET https https 在。那么您不需要任何站点扩展名或XDT文件。

有关更多信息,请参见此帖子。