首页 文章

使用SSL和Cookie身份验证重定向在Apache中进行ASP.NET Core托管

提问于
浏览
2

我已经制作了一个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网址?

1 回答

  • 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 部分,因为它是重定向请求的部分 . 另一个只是消费它们的问题 .

相关问题