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

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

我制作了一个 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/

相关文章:

ssl - 为什么我们需要在 Windows 中安装 .pfx 证书(而不是 .cer),然后才能使用客户端证书进行调用?

.htaccess - 将 IP 重定向或指向另一台服务器

python - Ubuntu Modoboa 从 NGINX 到 APACHE2 : HttpS 403 forbidden while HTTP is correctly accessible. (mod_WSGI)

linux - 我收到 "cannot assign requested address"错误

php - 仍然显示相同的错误: cURL error 60: SSL certificate problem: unable to get local issuer certificate

apache - 将 301 从目录重定向到单个文件

linux - unix下的多个输入文本文件作为stdin

apache - OctoPrint 通过 Apache 反向代理 400 错误登录请求

php - htaccess 在不同的机器上表现不一样

c++ - 简单的客户端/服务器,TCP/IP 加密消息流,SSL (C++)