代码之家  ›  专栏  ›  技术社区  ›  yW0K5o

生产中的重定向URL不正确

  •  0
  • yW0K5o  · 技术社区  · 8 年前

    我使用ASP.NET核心2。 我在startup.cs中使用此代码

    services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(options => 
    {
        options.LoginPath = new PathString("/Account/Login");
        options.AccessDeniedPath = new PathString(/Account/AccessDenied);
    
        options.ExpireTimeSpan = TimeSpan.FromMinutes(3 * 60 + 1);
    });
    

    我没有登录,所以我的网站重定向到

    https://localhost/Account/Login?ReturnUrl=%252Fbbb .

    它在开发中工作。我得到网址

    https://aaaaa.com/?ReturnUrl=%252Fbbb 在生产中。

    如何修复?我搜索了谷歌,但找不到任何东西。

    1 回复  |  直到 8 年前
        1
  •  0
  •   yW0K5o    8 年前

    my apache代理文件000-default.conf

    <VirtualHost *:80>
        ServerName aaaaa.com
    
        RewriteEngine On
        RewriteCond %{HTTPS} !=on
        RewriteRule ^/?(.*) https://%{SERVER_NAME}/ [R,L]
    </VirtualHost>
    <VirtualHost *:443>
        ProxyPreserveHost On
        ProxyPass "/" "http://localhost:5000/"
        ProxyPassReverse "/" "http://localhost:5000/"
        ErrorLog /var/log/httpd/aaaaa-error.log
        CustomLog /var/log/httpd/aaaaa-access.log common
        SSLEngine on
        SSLProtocol all -SSLv2
        SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:!RC4+RSA:+HIGH:+MEDIUM:!LOW:!RC4
        SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt
        SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key
    </VirtualHost> 
    

    startup.cs中的代码

    services.ConfigureApplicationCookie(options =>
    {
        options.LoginPath = new PathString("/Account/Login");
        options.AccessDeniedPath = new PathString(/Account/AccessDenied);
    
        options.Events.OnRedirectToLogin = context =>
        {
            LogManager.GetLogger(this.GetType()).Info("OnRedirectToLogin->RedirectUri: " + context.RedirectUri);
    
     #if DEBUG
            context.Response.Redirect(context.RedirectUri);
     #else
            string strURL = context.RedirectUri.ToLower();
    
            if (strURL.StartsWith("http://"))
            {
                strURL = strURL.Replace("http://", "https://", StringComparison.CurrentCultureIgnoreCase);
            }
    
            context.Response.Redirect(strURL);
    #endif
            return Task.CompletedTask;
        };
    
    });
    

    日志记录显示HTTP协议 http://aaaaa.com/Account/Logon?ReturnUrl=%252Fbbb 然后是阿帕奇 从URL剪切帐户/登录时重定向到HTTPS。

    解决方案很简单,将HTTP替换为HTTPS,这样Apache就不会重定向。

    爱德华和马克,谢谢你的提示!

    推荐文章