【问题标题】:Request URI in HTTPS in Google API in reverse proxy server with kestrel使用红隼的反向代理服务器中的 Google API 中的 HTTPS 请求 URI
【发布时间】:2017-10-19 05:35:13
【问题描述】:

背景: 使用 Kestrel 和 Apache 后面的 ASP.NET Core 作为反向代理。对本网站的所有流量使用 https 协议。

我的问题是,如何使用 Google API 在回调 uri 中强制使用 https 而不是 http 响应?

我在调用 Google API 后收到一条错误消息。错误消息是“错误:redirect_uri_mismatch”。我已经使用 https 作为协议在 Google API 控制台上正确设置了回调。

请求详情如下:

> response_type=code
client_id=myclientidnum.apps.googleusercontent.com
redirect_uri=http://mysub.domain.com/signin-google
scope=openid profile email
state=long_long_code

如果您有任何想法,请给我留言。

【问题讨论】:

    标签: apache api reverse-proxy kestrel-http-server


    【解决方案1】:

    当我尝试使用 Kestrel 通过反向代理为在 Apache 上运行的 ASP.NET Core Web 应用程序配置 Google 身份验证时,我还收到了 redirect_uri_mismatch 错误。

    错误是由于将授权重定向 URI 设置为 https。一旦我将其更改为http,Google 身份验证就起作用了。

    因此,假设您的域是 example.com,请将您的授权重定向 URI 更改为

    https://example.com/signin-google
    

    http://example.com/signin-google
    

    【讨论】:

    • 谢谢,金巴迪。但是你如何应对其他需要 https 回调的 web api 调用呢?例如微软。
    • Microsoft 不允许使用 http,所以我只输入了 https://example.com/signin-microsoft 并获得了身份验证。对于 Twitter,我也输入了 https://example.com/signin-twitter。对于 Facebook,我输入了 http://example.com/signin-facebook 并且它起作用了。到目前为止,我已经成功地启用了 Facebook、Google、Twitter 和 Microsoft 的身份验证。我的方法是首先尝试https,如果不起作用,请尝试http。到目前为止,一切顺利。
    • kimbaudi,我在 Microsoft API 控制台中使用 https 回调设置为 Microsoft 做的。但是回调的调用还是没有加密的。
    • @userIndulgeInDChord - 当您对 Microsoft 说“回调的调用仍然没有加密”时,这是否意味着 Microsoft 身份验证不适合您?或者它正在工作,但没有加密(或其他东西)?我在使用 Microsoft 启用 3rd 方身份验证时遇到问题,但我让它通过反向代理与 Kestrel 一起工作。也许我可以帮助你让它工作,但没有加密。你能详细说明一下吗?
    • 不用担心。我在配置有关 Microsoft Web api 设置的 startup.cs 时犯了一个错误。现在运行得很好。
    【解决方案2】:

    问题是服务器没有使用反向代理转发的标头,因此它请求的协议不正确。您需要:

    1. 确保您的反向代理正在设置 X-Forward 标头
    2. 启用 UseForwardedHeaders 中间件身份验证之前
    app.UseForwardedHeaders(new ForwardedHeadersOptions
    {
        ForwardedHeaders = 
            ForwardedHeaders.XForwardedFor | 
            ForwardedHeaders.XForwardedProto
    });
    
    app.UseAuthentication();
    

    https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-apache?view=aspnetcore-2.1&tabs=aspnetcore2x

    【讨论】:

      【解决方案3】:

      我有同样的问题,并通过下面的代码解决了这个问题。

                 app.Use(async (ctx, next) =>
                  {
                      ctx.Request.Scheme = "https";
                      await next();
                  });
      
                  ForwardedHeadersOptions forwardOptions = new 
                   ForwardedHeadersOptions
                  {
                      ForwardedHeaders = ForwardedHeaders.XForwardedFor | 
                      ForwardedHeaders.XForwardedProto,
                      RequireHeaderSymmetry = false
                  };
      
                  forwardOptions.KnownNetworks.Clear();
                  forwardOptions.KnownProxies.Clear();
      
                  app.UseForwardedHeaders(forwardOptions);
      

      现在 google 和 facebook 不支持生产应用程序中的 http 重定向,并且反向代理后面的 kestrel 在 http 上运行也面临同样的问题。我使用上面的代码强制所有对 https 的请求的架构(这是我在 https 反向代理后面的安全赌注)并且一切正常

      【讨论】:

        猜你喜欢
        • 2018-08-21
        • 2022-01-22
        • 2020-05-03
        • 2020-03-31
        • 2013-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多