【问题标题】:ASP.NET Core hosting in Apache using SSL and Cookie Authentication redirect使用 SSL 和 Cookie 身份验证重定向在 Apache 中托管 ASP.NET Core
【发布时间】:2017-12-04 01:43:19
【问题描述】:

我已经制作了一个 asp.net 核心应用程序,我正在尝试使用反向代理将它托管在 Apache 中。应用使用cookie认证:

app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
  AuthenticationScheme = "CookieAuthentication",
  LoginPath = new PathString("/Account/Login/"),
  AccessDeniedPath = new PathString("/Account/Forbidden/"),
  AutomaticAuthenticate = true,
  AutomaticChallenge = true
});

在 httpd.conf 中,我想使用一个仅带有自定义端口的 SSL 主机,该端口提供来自 Kestrel 的内容。

Listen 34567

<VirtualHost *:34567>
  ProxyPreserveHost On
  ProxyPass / http://127.0.0.1:5000/
  ProxyPassReverse / http://127.0.0.1:5000/
  SSLEngine on
  SSLProtocol all -SSLv3
  SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:!RC4+RSA:+HIGH:+MEDIUM:!LOW:!RC4
  SSLCertificateFile certs/server.crt
  SSLCertificateKeyFile certs/server.key
</VirtualHost>

当我使用 url https://testserver1:34567 时,它会重定向到 http://testserver1:34567/Account/Login/?ReturnUrl=%2F,这当然会给出 Bad Request。如果我通过将网址更改为 https 来更正网址,之后一切正常。

我怎样才能让它总是重定向到 https url?

【问题讨论】:

    标签: apache ssl redirect asp.net-core reverse-proxy


    【解决方案1】:

    我解决我的方法是将所有 http 请求(包括路由)重定向到 https 请求。

    这是我的整个 Apache 配置文件。

    <VirtualHost *:80>
        RewriteEngine On
        RewriteCond %{HTTPS} !=on
        RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
    </VirtualHost>
    
    <VirtualHost *:443>
        RequestHeader set X-Forwarded-Proto "https"
        ServerName mydomain.com
    
        ProxyPass / http://127.0.0.1:5000/
        ProxyPassReverse / http://127.0.0.1:5000/
        ErrorLog /var/log/httpd/netcore-error.log
        CustomLog /var/log/httpd/netcore-access.log common
        SSLEngine on
        SSLProtocol all -SSLv2
        SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:!RC4+RSA:+HIGH:+MEDIUM:!LOW:!RC4
        SSLCertificateFile /etc/letsencrypt/live/mydomain.com/cert.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/mydomain.com/privkey.pem
        SSLCertificateChainFile /etc/letsencrypt/live/mydomain.com/chain.pem
    </VirtualHost>
    

    那里的关键是VirtualHost *:80 部分,因为它是重定向请求的部分。另一个只是消耗它们的问题。

    【讨论】:

      猜你喜欢
      • 2013-02-14
      • 2017-03-30
      • 2017-02-15
      • 2018-02-24
      • 1970-01-01
      • 2018-12-30
      • 1970-01-01
      • 2019-11-30
      • 2017-05-01
      相关资源
      最近更新 更多