将规则重写为IIS以将HTTPS重定向到HTTP会导致null HttpContext.Request.ContentType [英] Rewrite Rule to IIS for HTTPS redirect to HTTP causes 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>
我为<$ c获得 null
$ c> HttpContext.Request.ContentType 当Postman发送到HTTP而不是HTTPS时,上面的内容生效。
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
result。
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.
参见这篇文章了解更多信息。
这篇关于将规则重写为IIS以将HTTPS重定向到HTTP会导致null HttpContext.Request.ContentType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!