Apache反向代理中的Google OAuth重定向URI

fivyi3re  于 2023-03-24  发布在  Apache
关注(0)|答案(3)|浏览(154)

我正在尝试使用Apache作为反向代理来设置一个Web服务器,该服务器连接到由kestrel托管的ASP.NET CORE Web应用程序。我已经遵循了以下教程:https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-apache?view=aspnetcore-3.1
该网站使用Google Calendar API来获取用户的日历信息。当我通过Visual Studio使用Kestrel托管它时,这很好用。但是,当我尝试通过Web服务器进行身份验证并选择Google帐户时,我被重定向到

127.0.0.1:5000/signin-oidc

这不是期望的结果。(127.0.0.1:5000被配置为Apache的ProxyPass和ProxyReverse)。
由于Web应用程序在没有Apache反向代理的情况下运行,我怀疑apache配置有一些问题。然而,这也可能是我使用的Google.Apis.Auth.AspNetCore3库的问题。我使用the integrationtests作为如何设置 * startup.cs * 和如何发出API请求的指南。
编辑:
所以我问了我正在使用的库的开发人员。重定向uri绑定到我监听的kestrel的端点。问题在这里:https://github.com/googleapis/google-api-dotnet-client/issues/1680
所以它只能工作,如果我能以某种方式让kestrel知道我的服务器的“实际”域名或公共IP...从我的研究,这似乎接近如果不是不可能的。我将寻找其他实现选项的授权和身份验证。

c9x0cxw0

c9x0cxw01#

找到解决方案了!
在我无休止的搜索中,我发现我已经删除了我的VirtualHost for apache的转发头。我还添加了ProxyPreserveHost选项。
我将虚拟主机更改为包括

RequestHeader set X-Forwarded-Proto https
ProxyPreserveHost On
ProxyPass / http://localhost:5000/
ProxyPassReverse / http://localhost:5000/
ServerName www.example.com
ServerAlias *.example.com
k97glaaz

k97glaaz2#

RequestHeader set X-Forwarded-Proto https
ProxyPreserveHost On

添加上述两个指令即可完成此工作
参考文献:
ProxyPreserveHost On
RequestHeader set X-Forwarded-Proto https

r6l8ljro

r6l8ljro3#

如果你使用的是网络核心,仅仅添加正确的配置到虚拟主机是不够的,你需要添加

app.UseForwardedHeaders(new ForwardedHeadersOptions
        {
            ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
        });
        
        app.Use((context, next) =>
        {
            if (context.Request.Headers.TryGetValue("X-Forwarded-Proto", out var protoHeaderValue) &&
                protoHeaderValue == "https")
            {
                context.Request.Scheme = "https";
            }
            return next();
        });

以及

.AddGoogle(options =>
            {
                    OnRedirectToAuthorizationEndpoint = context =>
                    {
                        context.Response.Redirect(context.RedirectUri.Replace("http://", "https://", StringComparison.OrdinalIgnoreCase));
                        return Task.CompletedTask;
                    },
                };

相关问题