将规则重写到 IIS 以将 HTTPS 重定向到 HTTP 导致 null HttpContext.Request.ContentType [英] Rewrite Rule to IIS for HTTPS redirect to HTTP causes null HttpContext.Request.ContentType

查看:12
本文介绍了将规则重写到 IIS 以将 HTTPS 重定向到 HTTP 导致 null HttpContext.Request.ContentType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Azure 使用 HTTPS 重定向站点扩展它有一个这样的 applicationhost.xdt:

I am using HTTPS redirect site extension for Azure which has an applicationhost.xdt like this:

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

I am getting null for HttpContext.Request.ContentType when Postman sends to HTTP instead of HTTPS and the above takes effect.

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

I test with this middleware like so:

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 的通用中间件解决方案.

I'd prefer to continue handling HTTP redirect in IIS as opposed to the common middleware solution for .Net Core.

有谁知道要添加到 applicationhost.xdt 以寻址的参数吗?

Does anyone know a parameter to add to applicationhost.xdt to address?

更新 1:为清楚起见:

当我调用 HTTP 并重定向到 HTTPS 时,会重现此问题.

This issue reproduces only when I call HTTP and a redirect to HTTPS occurs.

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

Conversely, when I call using HTTPS I get the expected Request.ContentType result.

使用此 在下面回答,我得到了同样的行为.我不再确定解决方案将是对applicationhost.xdt.也许我需要以另一种方式获取 Request.ContentType?

Using the solution provided in this Answer below, I get the same behavior. I am no longer sure the solution will be an edit to applicationhost.xdt. Perhaps I need to get the Request.ContentType another way?

推荐答案

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

Note that there is now a much simpler way of redirecting http traffic to https: under Custom domains, just set HTTPS Only to On. Then you don't need any site extension or xdt file.

请参阅 这篇文章了解更多信息.

See this post for more info.

这篇关于将规则重写到 IIS 以将 HTTPS 重定向到 HTTP 导致 null HttpContext.Request.ContentType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆