2016-10-16 73 views
0

在我的ASP.NET Core 1.0应用程序中,我使用Cookie中间件来提供我自己的登录屏幕。我跟着ASP.NET核心文档:https://docs.asp.net/en/latest/security/authentication/cookie.html带端口号的ASP.NET核心认证

Startup.cs

... 
app.UseCookieAuthentication(new CookieAuthenticationOptions 
{ 
    AuthenticationScheme = "MyCookieMiddlewareInstance", 
    LoginPath = new PathString("/Config/Login"), 
    AutomaticAuthenticate = true, 
    AutomaticChallenge = true, 
}); 
... 

我的项目现在结束,我把它发布到Linux环境。我已将Nginx配置为反向代理以将请求转发给我的ASP.NET应用程序。 我的配置将端口5000上的传入公共流量转发到我的Web应用程序正在侦听的当前端口。

的/ etc/nginx的/网站可用/默认

server { 
    listen 5000; 
    location/{ 
     proxy_pass http://localhost:4007; 
     proxy_http_version 1.1; 
     proxy_set_header Upgrade $http_upgrade; 
     proxy_set_header Connection keep-alive; 
     proxy_set_header Host $host; 
     proxy_cache_bypass $http_upgrade; 
    } 
} 

一切正常,因为它应该;但是当我尝试访问未经授权的页面时,我的应用程序中配置的身份验证选项会将页面转发到登录页面。不幸的是,它从网址中删除了端口号。当我手动添加端口号时,它可以工作。

当我请求http://'ip-server':5000/Config时,应用程序应该将我的请求重定向到http://'ip-server':5000/Config/Login?ReturnUrl =%2FConfig when I am没有登录。相反,它现在将我重定向到http://'ip-server'/ Config/Login?ReturnUrl =%2FConfig。因此它重定向到一个不存在的页面。

我需要更改哪些内容(在Nginx中的应用程序中?),以便它保持url中的端口号?我还没有在互联网上发现任何有关它的事情。

回答

1

我已经解决了这个问题。我需要在我的nginx的配置中使用$http_host而不是$host

server { 
    listen 5000; 
    location/{ 
     proxy_pass http://localhost:4007; 
     proxy_http_version 1.1; 
     proxy_set_header Upgrade $http_upgrade; 
     proxy_set_header Connection keep-alive; 
     proxy_set_header Host $http_host; 
     proxy_cache_bypass $http_upgrade; 
    } 
} 

现在重定向正确的,它使用的端口出现在URL中。