【问题标题】:GoogleOAuth2AuthenticationHandler + SSL Termination - redirect_uri_mismatchGoogleOAuth2AuthenticationHandler + SSL 终止 - redirect_uri_mismatch
【发布时间】:2020-01-23 09:29:19
【问题描述】:

我在 HAProxy 之后隐藏了一个应用程序。

存在 SSL 终止。 我的应用程序是https://myapp.domain.com,但在 HttpContext.Request 应用程序中将其识别为http://myapp.domain.com(没有 SSL,因为它在代理级别终止)。

问题是我使用 Microsoft.Owin.Security.Google 库。 当它使用 http 而不是 https 生成 redirect_uri 时。

我已经检查了 GoogleOAuth2AuthenticationHandler => ApplyResponseChallengeAsync() 方法的实现,它清楚地表明这个 redirect_uri 是从服务器端数据生成的:

    string str1 = this.Request.Scheme + Uri.SchemeDelimiter + (object) this.Request.Host + (object) this.Request.PathBase;
    string str2 = str1 + (object) this.Request.Path + (object) this.Request.QueryString;
    string str3 = str1 + (object) this.Options.CallbackPath;

问题是我怎样才能强制这个提供者获得一个有效的 url 方案?

【问题讨论】:

    标签: c# asp.net-mvc authentication oauth


    【解决方案1】:

    我找到了解决问题的方法。 此上下文中的 Request.Scheme 不是 HttpRequest 而是 IOwinRequest

    可以在 Startup.cs 文件中全局覆盖:

        app.Use((context, next) =>
        {
            context.Request.Scheme = context.Request.GetRealScheme(true);
            return next();
        });
    

    GetRealScheme() 是我的扩展方法:

        private const string HeaderProtoKey = "X-Forwarded-Proto";
    
        public static string GetRealScheme(this IOwinRequest request, bool skipPostfix = false)
        {
            if (request == null)
                return null;
    
            string headerProtocol = request.Headers[HeaderProtoKey] ?? string.Empty;
    
            string result = headerProtocol.ToLower().Contains("https")
                ? "https://"
                : request.Scheme;
    
            return skipPostfix
                ? result.Replace("://", string.Empty)
                : result;
        }
    

    需要注意的重要一点!

    此代码应身份验证设置之前应用。

    【讨论】:

      猜你喜欢
      • 2015-04-20
      • 2016-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-19
      • 1970-01-01
      相关资源
      最近更新 更多