将规则重写为IIS以将HTTPS重定向到HTTP会导致null HttpContext.Request.ContentType

问题描述 投票:0回答:1

我正在使用HTTPS redirect site extension for Azure,它有一个像这样的applicationhost.xdt:

<?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>

当邮递员发送到HTTP而不是HTTPS时,我正在为null获取HttpContext.Request.ContentType,以上内容生效。

我测试这个中间件是这样的:

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结果。

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

azure iis asp.net-core azure-web-sites
1个回答
2
投票

请注意,现在有一种更简单的方法可以将http流量重定向到https:在自定义域下,只需将HTTPS设置为On。然后,您不需要任何站点扩展或xdt文件。

有关更多信息,请参阅this post

© www.soinside.com 2019 - 2024. All rights reserved.