Rewrite Rule for IIS to redirect HTTPS to HTTP calls null HttpContext.Request.ContentType

I am using HTTPS redirect site extension for Azure which has 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>

      

I get null

for HttpContext.Request.ContentType

when Postman sends HTTP instead of HTTPS and this takes effect.

I am testing this middleware like this:

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}");
    }
} 

      

I would rather continue handling the HTTP redirect in IIS rather than a generic middleware solution for .Net Core

.

Does anyone know a parameter to add to applicationhost.xdt

for an address?

Update 1 : for clarity:

This issue is reproducible only when making an HTTP call and a redirect to HTTPS occurs.

Conversely, when I call with HTTPS

, I get the expected result Request.ContentType

.

Using the solution provided in this answer below , I get the same behavior. I'm not sure what the solution will be applicationhost.xdt

. Perhaps I need to do it in a Request.ContentType

different way?

0


source to share


1 answer


Note that there is now a much easier way to redirect HTTP traffic to https: under Custom Domains, just set HTTPS Only On. Then you don't need a site extension or xdt file.



See this post for details .

+1


source







All Articles