【发布时间】: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.ContentType 的 null。
我像这样使用这个中间件进行测试:
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