ASP.NET Core Azure App Service httpContext.Request.Headers[“Host”] Value

ⅰ亾dé卋堺 提交于 2019-12-11 03:53:51

问题


Faced strange behaviour today. We are hosting asp.net core 1.1 web app with Azure App Services and using subdomains that route to a specific controller or area. So in my SubdomainConstraint: IRouteConstraint I use

HttpContext.Request.Headers["Host"]

to get host name. That previously returned smth like that

mywebsite.com or subdomain.mywebsite.com 

Starting today (or a maybe yesterday) it started to return my App Service name instead of host name. On localhost everything works fine. Enumerating through

Context.Request.Headers

in one of my Views gives me on localhost:

Accept : 
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding : gzip, deflate, sdch, br
Accept-Language : ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4,ca;q=0.2
Cookie : .AspNetCore.Antiforgery....
Host : localhost:37202
User-Agent : Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 
(KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Upgrade-Insecure-Requests : 1

in Azure App Service:

Connection : Keep-Alive
Accept : text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding : gzip, deflate, sdch
Accept-Language : ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4,ca;q=0.2
Cookie : AspNetCore.Antiforgery....
Host : mydeploymentname:80
Max-Forwards : 10
User-Agent : Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Upgrade-Insecure-Requests : 1
X-LiveUpgrade : 1
X-WAWS-Unencoded-URL : /
X-Original-URL : /
X-ARR-LOG-ID : 9c76e796-84a8-4335-919c-9ca4rb745f4fefdfde
DISGUISED-HOST : mywebsite.com
X-SITE-DEPLOYMENT-ID : mydeploymentname
WAS-DEFAULT-HOSTNAME : mydeploymentname.azurewebsites.net
X-Forwarded-For : IP:56548
MS-ASPNETCORE-TOKEN : a97b93ba-6106-4301-87b2-8af9a929d7dc
X-Original-For : 127.0.0.1:55602
X-Original-Proto : http

I can get what I need from

Headers["DISGUISED-HOST"]

But having problems with redirects to a login page, it redirects to the wrong URL with my deployment name.

Wondering if I could mess something up anywhere. But we've made last deployment like a few days ago and it worked fine after that.


回答1:


This is caused by a regression in AspNetCoreModule deployed to a small number of apps in Azure App Service. This issue is being investigated. Please follow this thread for status.

Here is a workaround you can use until the fix is deployed: in your Configure method (typically in startup.cs), add the following:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        app.Use((ctx, next) =>
        {
            string disguisedHost = ctx.Request.Headers["DISGUISED-HOST"];
            if (!String.IsNullOrWhiteSpace(disguisedHost))
            {
                ctx.Request.Host = new Microsoft.AspNetCore.Http.HostString(disguisedHost);
            }
            return next();
        });

        // Rest of your code here...

    }


来源:https://stackoverflow.com/questions/44677338/asp-net-core-azure-app-service-httpcontext-request-headershost-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!