【问题标题】:Incorrect redirect URL in Production生产中的重定向 URL 不正确
【发布时间】:2018-07-27 08:12:50
【问题描述】:

我使用 ASP .NET CORE 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 在生产中。

如何解决?我在 Google 上进行了搜索,但找不到任何东西。

【问题讨论】:

  • Production 环境是什么?您是否在 IIS 中托管?您是如何发布到Production 环境的?
  • 你关注Host ASP.NET Core on Linux with Nginx配置你的UBuntu了吗?对于转发请求,您是否按照本文所述在error handing 之后添加app.UseForwardedHeaders
  • 可能会钩入OnRedirectToLogin 事件以确认重定向网址。
  • 我建议你尝试创建一个中间件来记录请求URL,并检查https://aaaaa.com/?ReturnUrl=%252Fbbb之前的哪个URL。我认为登录过程中有问题。也许 web.config 中的 stdoutLogEnabled 就足够了。
  • @yW0K5o 该链接是为了演示使用事件而不是设置StatusCode,您应该看到RedirectUri 设置为什么。

标签: asp.net-core


【解决方案1】:

我的 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,然后是 Apache 从 URL 中删除帐户/登录时重定向到 HTTPS。

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

Edward 和 MarkG,感谢您的提示!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 2016-05-05
    • 2019-08-07
    • 1970-01-01
    • 2012-09-27
    相关资源
    最近更新 更多