我制作了一个 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,这当然会给出一个错误请求。如果我通过将其更改为 https 来更正 url,之后一切正常。
如何让它始终重定向到 https url?
最佳答案
我解决问题的方法是将所有 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
部分,因为它是重定向请求的部分。另一个只是消耗它们的问题。
关于apache - 使用 SSL 和 Cookie 身份验证重定向在 Apache 中托管 ASP.NET Core,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44840665/