C# GoogleOAuth2AuthenticationHandler+;SSL终止-重定向\u uri\u不匹配

C# GoogleOAuth2AuthenticationHandler+;SSL终止-重定向\u uri\u不匹配,c#,asp.net-mvc,authentication,oauth,C#,Asp.net Mvc,Authentication,Oauth,我在HAProxy之后隐藏了一个应用程序 存在SSL终止。 我的应用程序只是在HttpContext中。请求应用程序将其识别为(没有SSL,因为它在代理级别终止) 问题是我使用的是Microsoft.Owin.Security.Google库。 当它使用http而不是https生成重定向uri时 我已经检查了GoogleOAuth2AuthenticationHandler=>ApplyResponseChallengeAsync()方法的实现,它清楚地表明这个重定向uri是从服务器端数据生成的

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

存在SSL终止。 我的应用程序只是在HttpContext中。请求应用程序将其识别为(没有SSL,因为它在代理级别终止)

问题是我使用的是Microsoft.Owin.Security.Google库。 当它使用http而不是https生成重定向uri时

我已经检查了GoogleOAuth2AuthenticationHandler=>ApplyResponseChallengeAsync()方法的实现,它清楚地表明这个重定向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方案?

我找到了解决问题的方法。 在此上下文中,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;
    }
有一件重要的事情要注意

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