带有端口号的 ASP.NET Core 身份验证

Tho*_*ele 2 authentication asp.net-mvc port nginx asp.net-core

在我的 ASP.NET Core 1.0 应用程序中,我使用 Cookie 中间件来提供我自己的登录屏幕。我遵循了 ASP.NET Core 文档:https : //docs.asp.net/en/latest/security/authentication/cookie.html

启动文件

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

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

/etc/nginx/sites-available/default

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

我需要更改什么(在应用程序中?在 Nginx 中?)以便它在 url 中保留端口号?我还没有在互联网上找到任何关于它的信息。

Tho*_*ele 6

我已经解决了这个问题。我需要在我的 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;
   }
}
Run Code Online (Sandbox Code Playgroud)

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

  • 进一步阅读:/sf/ask/1079036731/ (2认同)