【问题标题】:ASP.NET Core authentication with port number带有端口号的 ASP.NET Core 身份验证
【发布时间】:2017-02-25 10:07:36
【问题描述】:

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

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/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;
    }
}

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

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

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

【问题讨论】:

    标签: asp.net-mvc authentication nginx asp.net-core port


    【解决方案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 中存在的端口。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 2018-07-07
    • 2017-03-30
    • 2018-06-25
    相关资源
    最近更新 更多