【问题标题】:Rewrite Rule to IIS for HTTPS redirect to HTTP causes null HttpContext.Request.ContentType将规则重写为 IIS 以将 HTTPS 重定向到 HTTP 导致 null HttpContext.Request.ContentType
【发布时间】:2018-07-30 00:44:51
【问题描述】:

我正在使用 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>

当 Postman 发送到 HTTP 而不是 HTTPS 并且上述内容生效时,我收到 HttpContext.Request.ContentTypenull

我像这样使用这个中间件进行测试:

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

【问题讨论】:

  • 您发送的是Content-Type 标头吗? :)
  • 是的,我可以在http 之后在邮递员中添加s。否则相同的确切请求..并重新发送。结果不同。

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


【解决方案1】:

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

请参阅this post 了解更多信息。

【讨论】:

  • 这是完美的解决方案。谢谢!
  • 但是,我得到了同样的行为。我删除了扩展。在 Azure 门户中将 HTTPS Only 设置为 On。重新启动网络应用程序。验证了它的工作原理,我确实被重定向到了 HTTPS。但是上面的中间件测试仍然为httpContext.Request.ContentType 返回一个empty string。甚至重新发布只是为了确定。
  • 那么问题与存在重定向的事实有关,还是与它使用 https 的事实有关?这些是非常不同的事情。例如如果您没有尝试将 http 重定向到 https,而是明确地转到 https,会发生什么情况。你看到同样的问题吗?如果是这样,您应该改写您的问题以删除与重定向相关的任何内容,因为它是一个红鲱鱼。
  • 当我不尝试使用重定向并直接调用 https 时,我会得到适当的httpContext.Request.ContentType。为清楚起见,将编辑解决方案可能不在applicationhost.xdt 中的新信息。
  • 我认为答案可能只是浏览器不知道如何重定向 POST 请求。因此,如果第二个请求实际上是作为 GET 来的,那么他们预计不会有 ContentType。如果你分析 http 流量和重定向,你应该能够验证这个理论。
猜你喜欢
  • 2015-08-10
  • 2016-12-14
  • 2013-06-02
  • 2014-11-04
  • 1970-01-01
  • 2019-10-05
  • 1970-01-01
  • 2015-12-26
  • 1970-01-01
相关资源
最近更新 更多